How to modify a value in one “cell” of a pandas data frame?

吃可爱长大的小学妹 提交于 2019-12-22 09:47:20

问题


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

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?


回答1:


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 and the warning raises to tell you that this would not change the value in the original dataframe 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)

Your way of setting value is fine, along with the following ones:

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


来源:https://stackoverflow.com/questions/26657378/how-to-modify-a-value-in-one-cell-of-a-pandas-data-frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!