python-3.x - 在 pandas 数据框中,给定两个列表列,根据第二列中的值访问第一列的值
问题描述
我有带有两个列表列的 Pandas 数据框:
df =
a label
[1, 2, 4, 5] [0, 0, 1, 0]
[100, 12, 23, 4] [1, 0, 0, 1]
最终,我想形成一个新的 column a_positive
,它也是一个包含所有元素的列表a
for ,label
所以1
:
df_output =
a label a_positive
[1, 2, 4, 5] [0, 0, 1, 0] [4]
[100, 12, 23, 4] [1, 0, 0, 1] [100, 4]
是否可以一起使用apply
和zip
ing 列表?
解决方案
尝试
df['new']=df.apply( lambda x : [s for s, t in zip(x['a'],x['label']) if t==1],axis=1)
df
Out[78]:
a label new
0 [1, 2, 4, 5] [0, 0, 1, 0] [4]
1 [100, 12, 23, 4] [1, 0, 0, 1] [100, 4]
推荐阅读
- vue.js - Vuejs 最佳实践防御来自在浏览器中编辑状态变量的接口攻击
- spring-boot - 如何更新 Gradle Spring Boot 依赖项
- docker - 使用 Docker Logs 看不到来自 VS Code 和 ASP.NET (Core) 的日志
- java - 无法将一对多关系对象保存到数据库中。如何保存表格列表?
- c# - 32 位进程接收消息但不执行它们
- python - Python - 使用 Redis 作为缓存层到 Mongo
- java - 如何更改flutter包的java版本?
- spring - 在 Kotlin 控制器中验证 Spring @PathVariable
- asp.net - ASP.NET Core 5.0 MVC - 在控制器中生成弹出消息
- tensorflow - OSError:SavedModel 文件不存在,使用 Tensorflow Lite