我正在尝试创建一个空数据框,并用另一个文件中存在的列填充空数据框。当我使用这个简单的代码时,它可以工作。
InputData['Quote'] = store['QUOTE_ID']
但是当我在代码之前添加一些条件时,它不接受条件并给出与store(原始)文件中相同的值。 下面是我试图使用的代码。
InputData = pd.read_csv('datalink')
OutputData=pd.DataFrame()
for i in xrange(len(InputData.index)):
if (i % 5000) == 0:
print i,
if ((InputData.ix[i,'WIN']=='Y') and ((InputData.ix[i,'COM_C']=='H') or (InputData.ix[i,'COM_C']=='S')) and(InputData.ix[i,'COM_L']!=0)):
OutputData['Quote']=InputData['QUOTE_ID']
OutputData['ComList']=InputData['COM_LISTPR']
OutputData['WIN']=1
OutputData['COM_C']=InputData['COM_C']
OutputData.to_csv(link,index=False)
QUOTE_ID WIN COM_C COM_L
1400453-IT N H 1.46E+05
1400453-IT N H 7.12E+04
1400453-IT N H 2.74E+04
1403796-IT Y S 3.11E+04
1400453-IT N M 3.12E+02
1403796-IT Y H 3.97E+04
1403796-IT Y H 3.97E+04
1403796-IT Y M 1.99E+02
1403796-IT Y M 1.99E+02
1403796-IT Y H 7.40E+04
1403796-IT Y H 7.40E+04
1403796-IT Y M 3.19E+02
1403796-IT Y M 3.19E+02
1403796-IT Y H 9.56E+04
Quote WIN COM_C COM_LISTPR
1403796-IT 1 S 3.11E+04
1403796-IT 1 H 3.97E+04
1403796-IT 1 H 3.97E+04
1403796-IT 1 H 7.40E+04
1403796-IT 1 H 7.40E+04
1403796-IT 1 H 9.56E+04
非常感谢提前
答案 0 :(得分:1)
Python代码 -
import pandas as pd
df = pd.read_csv('a.csv', delim_whitespace=True) # reading file
modified_df = df[(df['WIN'] == 'Y') & ((df['COM_C'] == 'S') | (df['COM_C'] ==
'H')) &
(df['COM_L'] !=
0)].copy()
modified_df['WIN'] = 1
print(modified_df)
输出 -
QUOTE_ID WIN COM_C COM_L
3 1403796-IT 1 S 31100
5 1403796-IT 1 H 39700
6 1403796-IT 1 H 39700
9 1403796-IT 1 H 74000
10 1403796-IT 1 H 74000
13 1403796-IT 1 H 95600