首页 > 解决方案 > 根据正在检查的条件在 Dataframe 中创建一个新列。我想检查一个列的值是否有多个值,然后为其分配一个值

问题描述

这是我现在正在做的事情:

df1.loc[df1['Product'] == "Bat",'Category'] = "Sports"
df1.loc[df1['Product'] == "Medicine",'Category'] = "Pharma"
df1.loc[df1['Product'] == "shoes",'Category'] = "Fashion"

但现在我需要使用同一条线检查其他东西,比如球、衣服等

df1.loc[df1['Product'] == ["Bat" or "Ball"],'Category'] = "Sports"

这样做时会出现以下错误:ValueError:Series 的真值是不明确的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

有没有更好的方法,我想对大量产品进行检查?

标签: python-3.xpandasdataframe

解决方案


推荐阅读