简体   繁体   中英

groupby that contain more than one element

I am trying to recover in a DataFrame the rows of the groups (by TYPE) that contain more than one element.

TYPE VALEUR
M1   A
M1   B
M2   A

the result should be :

TYPE VALEUR
M1   A
M1   B

Thanks!

Have no idea for an elegant solution. Here's my stupid one:

In [149]: m = df.groupby('TYPE').size() > 1

In [151]: df[df['TYPE'].map(m)]
Out[151]: 
  TYPE VALEUR
0   M1      A
1   M1      B

You could also use ix if you set TYPE as the index first:

df.set_index("TYPE", inplace=True)
df.ix[df.groupby(level=0).size() > 1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM