简体   繁体   English

Pandas DataFrame:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值

[英]Pandas DataFrame: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

I know there are tons of posts about this warning, but I couldn't find a solution to my situation. 我知道有很多关于这个警告的帖子,但我无法找到解决方案。 Here's my code: 这是我的代码:

df.loc[:, 'my_col'] = df.loc[:, 'my_col'].astype(int)
#df.loc[:, 'my_col'] = df.loc[:, 'my_col'].astype(int).copy()
#df.loc[:, 'my_col'] = df['my_col'].astype(int)

It produces the warning: 它产生警告:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值。 Try using .loc[row_indexer,col_indexer] = value instead 尝试使用.loc [row_indexer,col_indexer] = value

Even though I changed the code as suggested, I still get this warning? 即使我按照建议更改了代码,我仍然会收到此警告? All I need to do is to convert the data type of one column. 我需要做的就是转换一列的数据类型。

**Remark: ** Originally the column is of type float having one decimal (example: 4711.0). **备注:**最初该列的类型为float,具有一位小数(例如:4711.0)。 Therefore I change it to integer (4711) and then to string ('4711') - just to remove the decimal. 因此我将其更改为整数(4711)然后更改为字符串('4711') - 只是为了删除小数。

Appreciate your help! 感谢您的帮助!

Update: The warning was a side effect on a filtering of the original data that was done just before. 更新:该警告对过滤之前完成的原始数据的过滤产生了副作用。 I was missing the DataFrame.copy(). 我错过了DataFrame.copy()。 Using the copy instead, solved the problem! 使用副本代替,解决了问题!

df = df[df['my_col'].notnull()].copy()
df.loc[:, 'my_col'] = df['my_col'].astype(int).astype(str)
#df['my_col'] = df['my_col'].astype(int).astype(str) # works too!

I think need copy and omit loc for select columns: 我认为需要copy并省略选择列的loc

df = df[df['my_col'].notnull()].copy()
df['my_col'] = df['my_col'].astype(int).astype(str)

Explanation : 说明

If you modify values in df later you will find that the modifications do not propagate back to the original data ( df ), and that Pandas does warning. 如果稍后修改df值,您会发现修改不会传播回原始数据( df ),并且Pandas会发出警告。

another way is to disable chained assignments, which works on your code without the need to create a copy : 另一种方法是禁用链式赋值,它可以在您的代码上运行, 而无需创建副本

# disable chained assignments
pd.options.mode.chained_assignment = None 

If you need to change the data type of a single column, it's easier to address that column directly: 如果需要更改单个列的数据类型,则可以更直接地处理该列:

df['my_col'] = df['my_col'].astype(int)

Or using .assign : 或者使用.assign

df = df.assign(my_col=lambda d: d['my_col'].astype(int))

The .assign is useful if you only need the conversion once, and don't want to alter your df outside of that scope. 如果您只需要转换一次,并且不希望在该范围之外更改您的df ,则.assign非常有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SettingWithCopyWarning:试图在来自 DataFrame 的切片副本上设置一个值警告 - SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame WARNING SettingWithCopyWarning:试图在DataFrame的切片副本上设置一个值 - SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 对于循环错误,SettingWithCopyWarning:正在尝试在 DataFrame 中的切片副本上设置值 - For loop error, SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame Python:SettingWithCopyWarning:尝试在DataFrame的切片副本上设置值 - Python: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 更改熊猫列中的值:SettingWithCopyWarning:正在尝试在数据帧错误中的切片副本上设置值 - Changing values in pandas columns: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame error 试图在 DataFrame 的切片副本上设置一个值。- pandas - A value is trying to be set on a copy of a slice from a DataFrame. - pandas Python Pandas 警告:试图在 DataFrame 的切片副本上设置值 - Python Pandas Warning: A value is trying to be set on a copy of a slice from a DataFrame pandas 值正在尝试在 DataFrame 的切片副本上设置 - pandas value is trying to be set on a copy of a slice from a DataFrame python pandas:尝试在DataFrame的切片副本上设置一个值 - python pandas: A value is trying to be set on a copy of a slice from a DataFrame pandas 错误:正在尝试在 DataFrame 中的切片副本上设置值 - pandas error: value is trying to be set on a copy of a slice from a DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM