SettingWithCopyWarning:试图在复制数据的切片副本上设置值
python 536
原文标题 :SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a copied data
我知道网络上有很多关于这个特定场景的问题(和答案),但我仍然无法理解在这种情况下是什么原因造成的将被修改
执行此代码时
df = raw.copy() # making a copy of dataframe raw
df['new col'] = ''
for i in range(len(df)):
df['new col'].loc[i] = 'some thing'
我收到了这个警告
SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
self._setitem_single_block(indexer, value, name)
警告是否指的是复制数据帧df
(而不是原始数据帧raw
)上的更改?处理这种不必要的警告的最佳方法是什么(在这种情况下,我故意想要对复制的数据帧进行更改)