python - get groups that contain all needed values
问题描述
df = pd.DataFrame({'A' : ['bar', 'bar', 'bar', 'foo',
'foo', 'foo'],
'B' : [1, 2, 3, 4, 5, 6],
'C' : [2.0, 5., 8., 1., 2., 9.]})
>>> df
A B C
0 bar 1 2.0
1 bar 2 5.0
2 bar 3 8.0
3 foo 4 1.0
4 foo 5 2.0
5 foo 6 9.0
How can I get the groups with both neededVals = [1.0,2.0]
in C if I groupby('A')
:
3 foo 4 1.0
4 foo 5 2.0
5 foo 6 9.0
And just those values as well:
3 foo 4 1.0
4 foo 5 2.0
解决方案
我认为需要比较set
和GroupBy.transform
过滤boolean indexing
:
neededVals = [1.0,2.0]
df = df[df.groupby('A')['C'].transform(lambda x: set(x) >= set(neededVals))]
print (df)
A B C
3 foo 4 1.0
4 foo 5 2.0
5 foo 6 9.0
详情:
print (df.groupby('A')['C'].transform(lambda x: set(x) >= set(neededVals)))
0 False
1 False
2 False
3 True
4 True
5 True
Name: C, dtype: bool
第二个首先过滤掉不必要的行isin
,然后比较相等性:
df = df[df['C'].isin(neededVals)]
df = df[df.groupby('A')['C'].transform(lambda x: set(x) == set(neededVals))]
print (df)
A B C
3 foo 4 1.0
4 foo 5 2.0
推荐阅读
- java - 如何将android库项目转换为fat jar
- node.js - 使用另一台主机中的副本集连接到 mongodb 集群时出错
- google-maps - 使用电子表格中的 API 密钥进行身份验证
- r - 在 R 中运行样本大小不等的两样本 t.test
- android - Kotlin 不理解 ViewModelProviders.of(activity ?: fragment)
- c# - 无法转换类型 func
布尔 - python - Scrapy无法通过xpath找到数据
- visual-studio - 没有管理员权限的全局 TypeScript 安装?
- javascript - 使类元素固定到具有响应性的位置
- javascript - 在生产 Firebase 中找不到模块“styled-jsx/style”