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
如果我neededVals = [1.0,2.0]
,如何在C中同时获得包含groupby('A')
的群组:
3 foo 4 1.0
4 foo 5 2.0
5 foo 6 9.0
还有这些价值观:
3 foo 4 1.0
4 foo 5 2.0
答案 0 :(得分:1)
我认为需要将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