A value is trying to be set on a copy of a slice from a DataFrame

时间:2022-07-28 16:17:52

在为DataFrame对象新增一列数据时,报错“A value is trying to be set on a copy of a slice from a DataFrame”,原因是操作的数据不是原始数据,而是原始数据的视图。

解决方案:在新增列之前,显示复制原始数据。示例如下:

data4 = train_data[['uid', 'forward_count', 'like_count']].copy()
data4['forward_like_rate'] = data4['forward_count'] / (data4['like_count'] + 1)