中的SettingWithCopyWarning

中的SettingWithCopyWarning

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

问题描述

我有一个数据框,如下所示:

I have a dataframe as shown below:

df =
           index     P01  unten   oben     RV   R2_simu
2014-05-23 03:00:00  0.0    0.0    0.9    0.8         0
2014-05-23 06:00:00  0.5    0.7    1.4    0.1         0
2014-05-23 07:00:00  1.0    2.4    2.4    0.6         0
2014-05-23 08:00:00 0.55   15.7   28.0    0.3         0
....

然后尝试循环:

for i in range(0, len(df)):

    if df.P01[i] >= df.RV[i]:
        df.R2_simu[i] = 0

    elif df.P01[i] < df.RV[i]:
        df.R2_simu[i] = df.RV[i]
    else:
        pass

我希望收到如下所示的新数据框,

I expect to receive a new dataframe as shown below,

df =
           index     P01  unten   oben     RV   R2_simu
2014-05-23 03:00:00  0.0    0.0    0.9    0.8       0.8
2014-05-23 06:00:00  0.5    0.7    1.4    0.1         0
2014-05-23 07:00:00  1.0    2.4    2.4    0.6         0
2014-05-23 08:00:00 0.55   15.7   28.0    0.6       0.6

但是,我收到消息 SettingWithCopyWarning ,我尝试重写

however, I get the message SettingWithCopyWarning, I try to rewrite

 df.R2_simu[i] = df.RV[i]

 df.R2_simu[i] = df.RV[i].copy()

但是看来问题仍然存在.

But it seems the problem still exists.

有人知道如何处理吗?提前致谢!

Does anyone know how to deal with it? Thanks in advance!

推荐答案

尝试使用 loc 索引,这可能是在内部创建和写入df副本的原因.将循环更改为

Try setting the values on the dataframe with the loc indexing, this can be the reason that internally a copy of df is created and written to. Change your loop to

for i in range(0, len(df)):

    if df.P01[i] >= df.RV[i]:
        df.loc[i,"R2_simu"] = 0

    elif df.P01[i] < df.RV[i]:
        df.loc[i,"R2_simu"] = df.RV[i]
    else:
        pass

更好的是您不使用循环,而是使用矢量访问:

Even better is you don't use a loop, but vector access:

df.loc[df.loc[:,"P01"] >= df.loc[:,"RV"],"R2_simu"] = 0
df.loc[df.loc[:,"P01"] < df.loc[:,"RV"],"R2_simu"] = df.loc[df.loc[:,"P01"] < df.loc[:,"RV"],"RV"]

从内到外的解释

df.loc[:, "col"] =>占用每一行:和每一列col

df.loc[:, "col"] => take every row :, and column col

df.loc[x1 >= x2, "R2_simu"] =>仅考虑x1 >= x2R2_simu

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

09-03 04:42