我有一个DataFrame:
APPLICATION COUNT K3_Code_Sender K3_Code_Receiver TWO
0 BCCTBL_2 0.0 0 287 x
1 BDLSMF01 0.0 9996 0 x
2 BEX00003 0.0 762 0 x
3 BEX01910 711762.0 762 0 x
4 BEX02955 60.0 0 287 x
5 BEX02970 12058.0 762 387 x
6 BEX03148 179.0 0 0 x
在执行完迭代和if语句后(row [4] = row ['TWO'],它只是为了检查列的位置而已)
for index, row in df_fin_k.iterrows():
if row['K3_Code_Sender'] != '0' and row['K3_Code_Receiver'] != '0':
df_fin_k.at[index, row[4]] = 2
else:
df_fin_k.at[index, row['TWO']] = 1
我获取新列,而不是将值设置为两个列:
APPLICATION COUNT K3_Code_Sender K3_Code_Receiver TWO x
0 BCCTBL_2 0.0 0 287 x 1.0
1 BDLSMF01 0.0 9996 0 x 1.0
2 BEX00003 0.0 762 0 x 1.0
3 BEX01910 711762.0 762 0 x 1.0
4 BEX02955 60.0 0 287 x 1.0
5 BEX02970 12058.0 762 387 x 2.0
6 BEX03148 179.0 0 0 x 1.0
如何解决此问题,我们将获得:
APPLICATION COUNT K3_Code_Sender K3_Code_Receiver TWO
0 BCCTBL_2 0.0 0 287 1.0
1 BDLSMF01 0.0 9996 0 1.0
2 BEX00003 0.0 762 0 1.0
3 BEX01910 711762.0 762 0 1.0
4 BEX02955 60.0 0 287 1.0
5 BEX02970 12058.0 762 387 2.0
6 BEX03148 179.0 0 0 1.0
答案 0 :(得分:2)
尝试一次
for index, row in df_fin_k.iterrows():
if row['K3_Code_Sender'] != '0' and row['K3_Code_Receiver'] != '0':
df_fin_k.at[index, 'TWO'] = 2
else:
df_fin_k.at[index, 'TWO'] = 1
我已将df_fin_k.at[index, row['TWO']] = 1
替换为df_fin_k.at[index, 'TWO'] = 1
答案 1 :(得分:1)
我尝试过:
for index , row in df.iterrows():
if row['K3_Code_Sender'] + row['K3_Code_Receiver'] == 0:
df['TWO'][index] = 2
else:
df['TWO'][index] = 1