Python Pandas SettingWithCopyWarning

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26724378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:56:20  来源:igfitidea点击:

Pandas SettingWithCopyWarning

pythonpandaswarningschained-assignment

提问by ba_ul

Python 3.4 and Pandas 0.15.0

Python 3.4 和 Pandas 0.15.0

df is a dataframe and col1 is a column. With the code below, I'm checking for the presence of the value 10 and replacing such values with 1000.

df 是一个数据框, col1 是一列。使用下面的代码,我正在检查值 10 是否存在并将这些值替换为 1000。

df.col1[df.col1 == 10] = 1000

Here's another example. This time, I'm changing values in col2 based on index.

这是另一个例子。这一次,我根据索引更改 col2 中的值。

df.col2[df.index == 151] = 500

Both these produce the warning below:

这两者都会产生以下警告:

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

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

Finally,

最后,

cols = ['col1', 'col2', 'col3']
df[cols] = df[cols].applymap(some_function)

This produces a similar warning, with an added suggestion:

这会产生一个类似的警告,并附加一个建议:

Try using .loc[row_indexer,col_indexer] = value instead

I'm not sure I understand the discussion pointed to in the warnings. What would be a better way to write these three lines of code?

我不确定我是否理解警告中指出的讨论。编写这三行代码的更好方法是什么?

Note that the operations worked.

请注意,这些操作有效。

采纳答案by Paul H

The issue here is that: df.col1[df.col1 == 10]returns a copy.

这里的问题是:df.col1[df.col1 == 10]返回一个副本。

So I would say:

所以我会说:

row_index = df.col1 == 10
# then with the form .loc[row_indexer,col_indexer]
df.loc[row_index, 'col1'] = 100

回答by koelemay

Agreed with Paul about 'loc' usage.

同意 Paul 关于 'loc' 的用法。

For your applymap case you should be able to do this:

对于您的 applymap 案例,您应该能够做到这一点:

cols = ['col1', 'col2', 'col3']
df.loc[:, cols] = df[cols].applymap(some_function)