python - gropuby and remove specified groups in pandas DataFrame -
i have pandas dataframe:
df=pd.dataframe({'a':[1,1,2,2,3,3],'b':['c','t','k','c','c','k']})
i need group df , remove groups b ='t'. pandas groupby
syntax this? in example answer groups 2 , 3.
a groupby/filter
work here (filter return groups meet condition). so, example, following:
>>> df.groupby('a').filter(lambda x: (x['b'] != 't').all()) b 2 2 k 3 2 c 4 3 c 5 3 k
(x['b'] != 't').all()
allows keep group if there no rows 't' in column b
or write filter following way: create booleen series based on whether row of column b 't'. if sum booleen series, sum greater 0 if of elements 't':
>>> df.groupby('a').filter(lambda x: (x['b'] == 't').sum() == 0) b 2 2 k 3 2 c 4 3 c 5 3 k
there other ways write filter condition have same flavor.
Comments
Post a Comment