Python 熊猫:SettingWithCopyWarning
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23002762/
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
Pandas: SettingWithCopyWarning
提问by Jason
I'd like to replace values in a Pandas
DataFrame
larger than an arbitrary number (100 in this case) with NaN
(as values this large are indicative of a failed experiment). Previously I've used this to replace unwanted values:
我想将Pandas
DataFrame
大于任意数字(在本例中NaN
为100)的值替换为(因为如此大的值表示实验失败)。以前我用它来替换不需要的值:
sve2_all[sve2_all[' Hgtot ng/l'] > 100] = np.nan
However, I got the following error:
但是,我收到以下错误:
-c:3: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
C:\Users\AppData\Local\Enthought\Canopy32\User\lib\site-packages\pandas\core\indexing.py:346: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
self.obj[item] = s
From this StackExchange question, it seems that sometimes this warning can be ignored, but I can't follow the discussion well enough to be certain whether this applies to my situation. Is the warning basically letting me know that I'll be overwriting some of the values in my DataFrame
?
从这个 StackExchange question,似乎有时可以忽略此警告,但我无法很好地遵循讨论以确保这是否适用于我的情况。警告基本上是让我知道我将覆盖我的某些值DataFrame
吗?
Edit: As far as I can tell, everything behaved as it should. As a follow up is my method of replacing values non-standard? Is there a better way to replace values?
编辑:据我所知,一切都表现得如此。作为跟进是我替换非标准值的方法吗?有没有更好的方法来替换值?
采纳答案by Andy Hayden
As suggested in the error message, you should use loc to do this:
正如错误消息中所建议的,您应该使用 loc 来执行此操作:
sve2_all.loc[sve2_all['Hgtot ng/l'] > 100] = np.nan
The warning is here to stop you modifying a copy (here sve2_all[sve2_all[' Hgtot ng/l'] > 100]
is potentiallya copy, and if it is then any modifications would not change the original frame. It could be that it works correctly in some cases but pandas cannot guarantee it will work in all cases... use at your own risk (consider yourself warned! ;) ).
该警告是来阻止你修改副本(在这里sve2_all[sve2_all[' Hgtot ng/l'] > 100]
是潜在的副本,如果是则进行任何修改不会改变原有的框架。这可能是因为它正确地工作在某些情况下,但大熊猫不能保证它会在所有的工作情况......使用风险自负(考虑自己警告!;))。
回答by Marshall Farrier
I was getting this warning while trying to reset the contents of an entire DataFrame but couldn't resolve it using loc
or iloc
:
我在尝试重置整个 DataFrame 的内容时收到此警告,但无法使用loc
or解决它iloc
:
df.loc[:, :] = new_values # SettingWithCopyWarning
df.iloc[:, :] = new_values # SettingWithCopyWarning
But resolving to the ndarray contained as data solved the problem:
但是解析为数据包含的 ndarray 解决了问题:
df.values[:, :] = new_values # no warnings and desired behavior
回答by George Zoto
As it is suggested by other users, you can try:
根据其他用户的建议,您可以尝试:
myindex = sve2_all[' Hgtot ng/l'] > 100
sve2_all.loc[myindex, 'yourcolumn'] = np.nan
Keep in mind that if you run into problems creating pivot tables (pivot_table row
keyword not supported by pandas 0.16.0 #417
) you should use the new syntax of index and columns instead of rows and cols. https://github.com/yhat/ggplot/issues/417
请记住,如果您在创建数据透视表时遇到问题(pivot_tablerow
关键字不受支持pandas 0.16.0 #417
),您应该使用索引和列的新语法,而不是行和列。https://github.com/yhat/ggplot/issues/417
See also:
也可以看看:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
回答by ilyes
---Problem solved for me---
---为我解决了问题---
I had that warring error when i tried to convert float --> int even if i used the ".loc" command. my mistake was that i filtered my dataFrame (with masks) before the operation so the conversion occurred in only a small part of the dataframe item/column, the result was a mixed type column wich create a confuison. i solved the problem by converting the data frame before the masks (data filtration), i hope it will help.
当我尝试转换 float --> int 时,即使我使用了“.loc”命令,我也遇到了那个交战错误。我的错误是我在操作之前过滤了我的数据帧(带掩码),因此转换只发生在数据帧项目/列的一小部分,结果是一个混合类型的列,这会造成混淆。我通过在掩码之前转换数据帧(数据过滤)解决了这个问题,我希望它会有所帮助。