python - 如何根据另一列python将值附加到一行中?
问题描述
我有一个看起来像这样的数据框:
body label
the sky is blue [noun]
the apple is red. [noun]
Let's take a walk [verb]
我想根据正文列中是否有颜色将项目添加到标签列表中。
期望的输出:
body label
the sky is blue [noun, color]
the apple is red. [noun, color]
Let's take a walk [verb]
我努力了:
data.loc[data.body.str.contains("red|blue"), 'label'] = data.label.str.append('color')
解决方案
一种选择是apply
在系列上使用,然后直接附加到列表:
data.loc[data.body.str.contains('red|blue'), 'label'].apply(lambda lst: lst.append('color'))
data
body label
0 the sky is blue [noun, color]
1 the apple is red. [noun, color]
2 Let's take a walk [verb]
推荐阅读
- linux - 如何在 perl 脚本中使用 bash 管道
- java - 在另一个非 Spring 项目中重用 Spring-boot 子模块
- sql - Oracle 10g 中的可选条件
- reactjs - 如何防止在 if 语句中将 Null 添加到 className
- python - Kivy Treeview 位置问题
- python - 通过链接抓取网页。
- git - Git 合并 - 将没有冲突的文件添加到下一次提交
- python - 在张量流概率中指定 DirichletMultinomial
- html - Angular 6组件未显示
- python - list_of_post() 得到了一个意外的关键字参数“slug”