2017-02-09 68 views
2

我試圖在大熊貓數據幀中添加一個新列,然後按行更新列列的值的新熊貓列行:熊貓。如何更新由

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,爲什麼?我錯過了什麼?謝謝!

回答

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'])