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

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -