我正在尝试在pandas数据框中添加一个新列,然后逐行更新列的值:
my_df['col_A'] = 0
for index, row in my_df.iterrows():
my_df.loc[index]['col_A'] = 100 # value here changes in real case
print(my_df.loc[index]['col_A'])
my_df
但是,在打印输出中,col_A中的所有值仍为0,为什么?我错过了什么?谢谢!
答案 0 :(得分:5)
您要分配到此行my_df.loc[index]['col_A'] = 100
取而代之的是
my_df['col_A'] = 0
for index, row in my_df.iterrows():
my_df.loc[index, 'col_A'] = 100 # value here changes in real case
print(my_df.loc[index]['col_A'])