python - 熊猫根据 columnA.isin.list(columnB) 返回子集
问题描述
我有一个这样的数据框:
userid number weight mask
0 17cf2504d0c7 1 1 56
1 17cf2504d0c7 2 5 56
2 17cf2504d0c7 3 3 123
我只是想返回一个子集,其中“权重”出现在数字“掩码”列表中
userid number weight mask
1 17cf2504d0c7 2 5 56
2 17cf2504d0c7 3 3 123
我似乎很难使用迭代器,这似乎是对熊猫的浪费。
解决方案
好吧,一种方法是将整数转换为set
并使用集合减法。
mask_ = df.weight.astype(str).apply(set).sub(df['mask'].astype(str).apply(set)).str.len().eq(0)
然后
df.loc[mask_, :]
userid number weight mask
1 17cf2504d0c7 2 5 56
2 17cf2504d0c7 3 3 123
推荐阅读
- r - 基于正则表达式从两个列表中加入 data.frames
- java - 如何在 Java 命令行选项中将资源文件夹添加到类路径
- kotlin - h2.bindAddress 被忽略
- freertos - FreeRTOS - 硬故障分析 - 失败
- vba - 如何在 MS Project 2010 VBA 中判断用户是否删除了一个值?
- bootstrap-4 - 卡片标题 - 图标上的链接停止折叠
- swift - Swift 中的奇怪行为 switch-case 语句
- java - Flink 按字段 id 对记录进行分组的最佳方法
- java - 读取 XML 文件
- c# - 正则表达式拆分右括号,后跟“\ b”旁边的空格