首页 > 解决方案 > 大熊猫上的 SettingWithCopyWarning

问题描述

线

res2.loc[: , other_column_name] = res2.loc[: , other_column_name].apply(np.log) 

, where res2is a pandas.DataFrame, 给出以下警告:

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

经过几次尝试,我已经能够通过将行重写为这个来摆脱警告

res3 = res2.copy()
log = res2.loc[: , other_column_name].apply(np.log) 
res3.loc[: , other_column_name] = log

这显然不是一个优雅的解决方案,有没有更好的方法来解决这个问题?

编辑 1

res2 是这样创建的:

res2 = res1[res1.index <= last_valid_sentiment]

res2.head()

        sentiment  inf_neg
2001-11-28  -2.856371      0.534482
2001-12-26  -2.023266      0.473392
2002-01-30  -1.505361      0.444845
2002-02-27  -2.121079      0.433922
2002-03-27  -1.692547      0.469621

标签: pythonpandasdataframewarnings

解决方案


推荐阅读