Pandas 链式索引问题:SettingWithCopyWarning:

问题: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 inste
解决方案:学会识别链式索引,不惜一切代价避免使用链式索引
1. 如果要更改 原始数据,请使用单一赋值操作( loc):
data.loc[data.name == 'test' , 'score'] = 100
2. 如果想要一个副本,请确保强制让 Pandas 创建副本:
winners = data.loc[data.bid == data.price].copy()
winners.loc[304, 'bidder'] = 'therealname'
二、强烈不推荐直接关闭警告,不过还是提供一下关闭警告的设置方法:
pd.set_option('mode.chained_assignment', None)

参考链接:

解决方法:https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas

解决原理:https://zhuanlan.zhihu.com/p/41202576

猜你喜欢

转载自blog.csdn.net/yu1336199790/article/details/102932148