根据另一列的值选择分组

3
我希望提取具有type=a的客户:
customer type 
A       a       
A       b        
A       c        
B       b       
B       b        
C       c       
C       c       

My desired result is like this:

customer type
A       a
A       b 
A       c

有没有办法实现这个?
我尝试了以下方法,但我想我必须添加一些语句...
df.groupby("customer")
3个回答

4

为了获得最佳性能,请使用通过掩码过滤的customer布尔索引中使用Series.isin

df1 = df[df['customer'].isin(df.loc[df['type'].eq('a'), 'customer'])]
print (df1)
  customer type
0        A    a
1        A    b
2        A    c

或者,如果需要使用DataFrame.groupby测试是否至少有一个值与GroupBy.transformGroupBy.any匹配:

df1 = df[df['type'].eq('a').groupby(df['customer']).transform('any')]
print (df1)
  customer type
0        A    a
1        A    b
2        A    c

2

过滤器 不如Jez的解决方案高效,但看起来比较清晰明了。

df.groupby('customer').filter(lambda x : x['type'].eq('a').any())
Out[56]: 
  customer type
0        A    a
1        A    b
2        A    c

0
     df1=df[df['customer']=='A']
     print(df1)
     

请不要仅仅发布代码作为答案,还要提供解释您的代码是如何解决问题的。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Mark Rotteveel
当然。下次我会小心的。 - MANSI MALVANIYA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接