蟒蛇:为什么我越来越settingwithcopywarning(Why does this code throw a SettingWithCopyWarning? [duplicate])

编程入门 行业动态 更新时间:2024-10-21 17:21:21
蟒蛇:为什么我越来越settingwithcopywarning(Why does this code throw a SettingWithCopyWarning? [duplicate])

所以我试图创建一个新的列,指示指定的条件是否为True。 我希望列简单说明“1”或“0”。

这是我的代码:

data_sub = data_orig.loc[~pd.isnull(data_orig['Last_Audit_Date']), :] data_sub.reset_index(inplace=True) data_sub['PackageLengthFlag'] = (abs(data_sub.loc['AUDIT_Primary_Length'] - data_sub.loc[:, 'PKG_SUB_Length']) > threshold)

我认为True = 1和False = 0默认情况下,如果我将它转换为整数,对不对? (我以为我在某处说这个......)

这是我不断得到的警告:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

我读到:

如何在Pandas中处理SettingWithCopyWarning?

在熊猫片上设置值的正确方法

熊猫SettingWithCopyWarning

但我不认为他们做我正在寻找的东西。 任何人有任何建议? 我知道这个问题可能听起来很愚蠢,但仍然感谢任何帮助!

编辑我在创建data_sub的地方添加了两行代码。 希望有所帮助!

This question already has an answer here:

How to deal with SettingWithCopyWarning in Pandas? 13 answers

so I'm trying to create a new column that indicates whether or not the specified condition is True. I want the column to simply state "1" or "0".

Here's my code:

data_sub = data_orig.loc[~pd.isnull(data_orig['Last_Audit_Date']), :] data_sub.reset_index(inplace=True) data_sub['PackageLengthFlag'] = (abs(data_sub.loc['AUDIT_Primary_Length'] - data_sub.loc[:, 'PKG_SUB_Length']) > threshold)

I am thinking that True = 1 and False = 0 by default, if I convert it into integers, right? (thought I read somewhere saying this...)

And here's the warning that I keep getting:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

I read into:

How to deal with SettingWithCopyWarning in Pandas?

Correct way to set value on a slice in pandas

Pandas SettingWithCopyWarning

But I don't think they do what I am looking for. Anyone has any advice? I know this question may sound painfully stupid, but still appreciate any help!

Edit I've added the 2 lines of code where I created the data_sub. Hope that helps!

最满意答案

错误出现在上面的代码中,当您尝试提取某些数据帧子切片而没有创建完整副本时,因此您拥有的引用实际上是对另一个较大数据帧的一部分的引用。

没有太多关于你想要做什么的背景,只需事先做一个副本:

data_sub = data_sub.copy() data_sub['PackageLengthFlag'] = ((data_sub['AUDIT_Primary_Length'] \ - data_sub['PKG_SUB_Length']).abs() > threshold).astype(int)

调用df.abs对整个结果执行abs函数。 abs是一种不能处理pd.Series对象的香草python方法。

最后一个astype调用将结果转换为整数值。


以下是你正在做的一个例子:

df A_Key B_ID C_Key D_NA 0 123 22 343 23 1 121 23 45.4 52 x = df.iloc[[0], :] x A_Key B_ID C_Key D_NA 0 123 22 343 23 x.iloc[:, 0] += 2 /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py:517: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

您会看到生成的错误。 但在大多数情况下,原件仍应在不影响原件的情况下进行修改。 现在,先复制:

x = x.copy() x.iloc[:, 0] += 2 # no warning

而且你看到错误被抑制了。 有趣的是,当执行垂直子模式的类似操作时,不会看到相同的行为。 我相信熊猫通过返回完整的独立副本来巧妙地处理这个问题。

The error is in the code above this, when you try extracting some dataframe subslice without making a complete copy, so the reference you have is actually a reference to part of another larger dataframe.

Without much context on what you're trying to do, just make a copy beforehand:

data_sub = data_sub.copy() data_sub['PackageLengthFlag'] = ( data_sub['AUDIT_Primary_Length'] .sub(data_sub['PKG_SUB_Length']) .abs() .gt(threshold) .astype(int)

Call df.abs to perform the abs function on the entire result. abs is a vanilla python method that cannot handle pd.Series objects.

One final astype call converts the result to integral values.


Here's an example of what you're doing:

df A_Key B_ID C_Key D_NA 0 123 22 343 23 1 121 23 45.4 52 x = df.iloc[[0], :] x A_Key B_ID C_Key D_NA 0 123 22 343 23 x.iloc[:, 0] += 2 /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py:517: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead

You see the error generated. But in most cases, the original should still be modified without affecting the original. Now, copy first:

x = x.copy() x.iloc[:, 0] += 2 # no warning

And you see the error suppressed. Interestingly, the same behaviour is not seen when performing similar operations on vertical subslices. I believe pandas smartly handles this by returning a full independent copy.

更多推荐

本文发布于:2023-07-14 20:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107267.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蟒蛇   settingwithcopywarning   code   duplicate   SettingWithCopyWarning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!