如何修改一个“单元”中的值,的大 pandas 数据框? [英] How to modify a value in one "cell" of a pandas data frame?

查看:1389
本文介绍了如何修改一个“单元”中的值,的大 pandas 数据框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的问题。我想改变一个给定的列中的一个给定的行的pandas数据帧的值。我尝试以下列方式:

I have a very simple problem. I would like to change a value in a given column of a given row of a pandas data frame. I try to do it in the following way:

df['column3'].loc[this_date] = val

因此,我收到以下警告:

As a result I get the following warning:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

我对此警告的解释是,使用列名('column3')和 loc 我真的不访问(参考)数据帧的所需单元格。相反,我创建一个对象,它是单元格对象的副本,然后我尝试更改与此复制对象相关联的值。

My interpretation of this warning is that by using columns name ('column3') and loc I do not really access (refer to) the desired cell of the data frame. Instead, I create an object which is a copy of the "cell" object and then I try to change the value associated with this "copy-object".

不明白是它似乎工作。尽管大熊猫给我写了我试图修改副本的事实,我修改了原始数据框架。

What I do not understand is that it seems to work. In spite on the fact that pandas writes me that I try to modify the copy, I do modify the original data frame.

我的问题是如何确保我真的做我想做的事情,以及如何以正确的方式做,以使熊猫不抱怨?

My question is how to make sure that I am really doing what I would like to do and how to do it in a "correct" way so that the pandas does not complain?

推荐答案

p>得到警告的原因是 df 本身是一些其他dataframe对象的副本。我想你有一些原始的数据帧 df_origin 。您可以通过某些操作(如切片)从 df_origin 中获取 df 因此 df df_origin 的副本。然后你尝试设置一些值到 df ,警告提示告诉你这不会改变 df_origin
一个解决方案是在切片之前和之后使用单个变量来指向数据帧对象,如果你不关心df_origin。否则您可以通过 pd.set_option('mode.chained_assignment',无)

The reason of getting the warning is that df itself is a copy of some other dataframe object. I guess that you have some original dataframe df_origin. And you get df from df_origin by some operation such as slicing. So df is a copy of df_origin. Then you try to set some value to df, the warning raises to tell you that this would not change the value in df_origin. One solution is to use a single variable to point to the dataframe object before and after slicing if you don't care for df_origin. Otherwise you can suppress the warning by pd.set_option('mode.chained_assignment', None)

设置值以及以下内容:

 df.ix[this_date, 'column3] = val
 df.loc[this_date, 'column3'] = val
 df.at[this_date, 'column3'] = val

这篇关于如何修改一个“单元”中的值,的大 pandas 数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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