python - 熊猫替换不起作用(错误:试图在数据帧的切片副本上设置值)
问题描述
无法从类似问题中找到有效的答案。
我有一个数据框(数据),我从中创建了一个新的数据框(衣服)。
代码:
clothes = data[['title_orig', 'retail_price','units_sold']].copy()
输出:
**title_orig** **retail_price** **units_sold**
summer vintage flamingo top 14 100
women's casual sleeveless top 22 20000
cool t shirt for women 43 100
spring and summer women top 8 5000
当我尝试用''(空白)替换“女性”和“女性”时,它不起作用。首先,我收到此消息“正在尝试在 DataFrame 的切片副本上设置值”,但现在我得到零错误,并且“女性”和“女性”仍然存在。
clothes['title_orig'] = clothes['title_orig'].replace('women', '')
解决方案
修复那个转弯regex = True
clothes['title_orig'] = clothes['title_orig'].replace({'women' : ''}, regex = True)