pandas使用新值更新dataframe行

jak*_*ong 10 python pandas

我有一个文本文件已被读入熊猫 df1 = pandas.read_csv(r'fruits.txt', sep=',')

    item  freshness
0   apple    2.2
1   pear     0.0
Run Code Online (Sandbox Code Playgroud)

和一系列会产生结果的计算 apple = 2.3

是否可以这样做,pandas.update以便我可以更新数据框中freshnessfor 的值?apple2.3

jez*_*ael 15

您需要的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
Run Code Online (Sandbox Code Playgroud)