我有一个文本文件,已被df1 = pandas.read_csv(r'fruits.txt', sep=',')
item freshness
0 apple 2.2
1 pear 0.0
以及一系列会产生apple = 2.3
是否可以执行pandas.update
以便我可以将数据框中freshness
的{{1}}的值更新为apple
?
答案 0 :(得分:13)
你需要的IIUC loc
:
apple = 2.3
print df['item'] == 'apple'
0 True
1 False
Name: item, dtype: bool
df.loc[df['item'] == 'apple', 'freshness'] = apple
print df
item freshness
0 apple 2.3
1 pear 0.0