今天在执行代码的时候突然出现了报错:
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
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
直接翻译就是
一个值正试图被设置在一个DataFrame的分片的副本上
按它提示给的.loc()方法试了,没用反而有新的问题.
经过实验,最终加了一行代码搞定.
以下为报错的源代码,关键报错就在给新列赋值的环节
df['PRE'] = predict_labels
只要在赋值前加这么一行代码即可正常无报错:
df=df.copy()
即新代码为
df=df.copy() #加入这行
df['PRE'] = predict_labels
在我当前场景下完美解决,希望可以帮到各位~~