python-3.x - 根据正在检查的条件在 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()。
有没有更好的方法,我想对大量产品进行检查?
解决方案
推荐阅读
- java - 未找到依赖项 'org.springframework.boot:spring-boot-starter-data-jpa:2.5.3'
- javascript - Express Validator 中的可选对象字段但强制对象字段
- biztalk - 为什么双向发送端口不处理响应?
- android - Ionic 更改本机剪贴板复制/粘贴选择窗口布局
- vue.js - VueJS获取数组中复选框的ID
- python - 有没有办法在同一个 dockerfile 上运行节点的图像和 Python 的另一个图像?
- java - toString() 错误,无法解析方法 toString()
- html - 如何在不重新加载页面的情况下替换 HTML 块?
- regex - 在带有回车的文本中使用正则表达式
- python - Python PAO(前 pyomo.bilevel)求解器找到错误的解决方案