简体   繁体   English

根据标准过滤多索引df

[英]Filter multiindex df based on std

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

through the above I get the following multindex df: 通过上面,我得到以下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? 我只想保留std计数> 0的名称,你们有什么建议吗?

Use GroupBy.transform with std or SeriesGroupBy.nunique and filtering by boolean indexing : 使用GroupBy.transformstdSeriesGroupBy.nunique通过过滤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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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