pandas :SettingWithCopyWarning [英] Pandas: SettingWithCopyWarning

查看:77
本文介绍了 pandas :SettingWithCopyWarning的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用NaN替换大于任意数(在这种情况下为100)的Pandas DataFrame中的值(因为该值太大表示实验失败).以前,我使用它来替换不需要的值:

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:

sve2_all[sve2_all[' Hgtot ng/l'] > 100] = np.nan

但是,出现以下错误:

-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

此StackExchange问​​题,似乎有时会出现此警告可以忽略不计,但我无法完全按照讨论进行,无法确定这是否适用于我的情况.警告基本上是让我知道我将覆盖我的DataFrame中的某些值吗?

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?

据我所知,一切正常.作为后续措施,我的替换值方法是否非标准?有没有更好的方法来替换值?

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?

推荐答案

如错误消息中所建议,您应该使用loc来做到这一点:

As suggested in the error message, you should use loc to do this:

sve2_all.loc[sve2_all['Hgtot ng/l'] > 100] = np.nan

警告是为了阻止您修改副本(此处sve2_all[sve2_all[' Hgtot ng/l'] > 100]可能是潜在副本,如果为副本,则进行任何修改都不会更改原始框架.在某些情况下可以正常使用,但熊猫无法保证在所有情况下都可以使用...使用后果自负(请注意警告!;)).

The warning is here to stop you modifying a copy (here sve2_all[sve2_all[' Hgtot ng/l'] > 100] is potentially a 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! ;) ).

这篇关于 pandas :SettingWithCopyWarning的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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