pandas 使用新值更新数据框行

编程入门 行业动态 更新时间:2024-10-19 15:44:54
本文介绍了 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,以便可以将数据框中的apple的freshness值更新为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,您需要 loc :

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 使用新值更新数据框行

本文发布于:2023-10-26 23:43:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1531688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!