pandas用新值

时间:2016-04-10 14:54:18

标签: python pandas

我有一个文本文件,已被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

1 个答案:

答案 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