lorenzo
lorenzo

Reputation: 407

Filter multiindex df based on std

df.groupby(['name','cat'])['valtocount'].agg('count')

through the above I get the following multindex df:

name cat count
abc  a   1
     b   1
def  a   1
     c   2

I want to keep only the names where the std of count is >0 do you guys have any suggestion?

Upvotes: 0

Views: 25

Answers (1)

jezrael
jezrael

Reputation: 863701

Use GroupBy.transform with std or SeriesGroupBy.nunique and filtering by boolean indexing:

s = df.groupby(['name','cat'])['valtocount'].agg('count')
s1 = s[s.groupby(level=0).transform('std') > 0]
print (s1)
name  cat
def   a      1
      c      2
Name: valtocount, dtype: int64

s1 = s[s.groupby(level=0).transform('nunique') != 1]

Upvotes: 1

Related Questions