-
问题描述
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当使用
df['new_column'] = df['old_column']时会报上述错误,并建议使用df.loc[row_indexer,col_indexer] = value,但是我已经使用了df.loc还是报这个错,并且不再指定具体是哪行代码错了。 -
解决方案
SettingWithCopyWarning这一类型错误的原因是当你做修改时,pandas不确定你是否是对原数据进行修改。
此处SettingWithCopyWarning是因为
df的问题,不是df中某些列的问题。# 状况一 df.loc[:,'new_column'] = value # 状况二 df_c = df['old_c1'] df_c.loc[:,'new_c3'] = value # 出现本例错误 # 修改为 df_c = df['old_c1'].copy df_c.loc[:,'new_c3'] = value # 问题解决 -
References
(20200410已解决)警告未提及代码SettingWithCopyWarning已使用df.loc
最新推荐文章于 2025-01-09 08:43:16 发布
本文详细解析了在使用Pandas库时遇到的SettingWithCopyWarning警告的成因及解决办法,尤其针对使用.loc操作时依然出现警告的情况,提供了有效的解决方案。
Python3.8
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
您可能感兴趣的与本文相关的镜像
Python3.8
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
1756

被折叠的 条评论
为什么被折叠?



