pandas 使用新值更新数据框行 [英] pandas update dataframe row with new value

查看:78
本文介绍了 pandas 使用新值更新数据框行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a text file which has been read into pandas by df1 = pandas.read_csv(r'fruits.txt', sep=',')

    item  freshness
0   apple    2.2
1   pear     0.0

以及一系列得出apple = 2.3

是否可以执行pandas.update,以便可以将数据框中的applefreshness值更新为2.3?

Is it possible to do a pandas.update so that I can update the value of freshness for apple in the dataframe to 2.3?

推荐答案

IIUC,您需要

IIUC you need 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

这篇关于 pandas 使用新值更新数据框行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆