jojo12
jojo12

Reputation: 5133

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!

Upvotes: 0

Views: 1812

Answers (2)

bdiamante
bdiamante

Reputation: 17600

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]

Upvotes: 1

waitingkuo
waitingkuo

Reputation: 93924

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

Upvotes: 4

Related Questions