[英]setting value in a slice of a pandas df raises SettingWithCopyWarning
[英]SettingWithCopyWarning Pandas setting value in column based on condition
我目前的代码如下:
conditions = [
(df1['item-condition'] == '1'),
(df1['item-condition'] == '2'),
(df1['item-condition'] == '3'),
(df1['item-condition'] == '4')
]
choices = ['Used - Like New', 'Used - Very Good', 'Used - Good', 'Used - Acceptable']
df1['item-condition'] = np.select(conditions, choices)
我正在尝试将“项目条件”列中的值从 1 设置为像新的,从 2 设置为非常好等
但是使用此代码我收到错误: 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
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
我的代码到底错在哪里,是否可以重写它以不引发此警告?
编辑以显示示例数据:
item-condition
0 2
1 2
2 3
3 1
应改为:
item-condition
0 Used - Very Good
1 Used - Very Good
2 Used - Good
3 Used - Like New
将其转换为 numpy 并重新附加它
a = df1['item-condition'].to_numpy()
cond_list = [a=1,a=2,a=3,a=4]
df1['item-condition'] = np.select(cond_list,choices,'potato')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.