Pandas group filtering objects

I have a pandas dataframe

df.columns Index([u'car_id',u'color',u'make',u'year')] 

I would like to create a new FILTERABLE object that has a counter for each group (color, year, year);

 grp = df[['color','make','year']].groupby(['color','make','year']).size() 

which will return something like this

 color make year count black honda 2011 416 

I would like to be able to filter it, however, when I try this:

 grp.filter(lambda x: x['color']=='black') 

I get this error

TypeError: object 'function' is not iterable

How can I use a groupby object to filter strings?

+7
python pandas indexing group-by condition
source share
2 answers

I think you need to add reset_index and then output the DataFrame . Last use of boolean indexing :

 df = df[['color','make','year']].groupby(['color','make','year']) .size() .reset_index(name='count') df1 = df[df.color == 'black'] 
+6
source share

Option 1
Filter ahead of time

 cols = ['color','make','year'] df[df.color == 'black', cols].grouby(cols).size() 

Option 2 Use xs for cross sections of the index

 cols = ['color','make','year'] grp = df[cols].groupby(cols).size() df.xs('black', level='color', drop_level=False) 

or

 df.xs('honda', level='make', drop_level=False) 

or

 df.xs(2011, level='year', drop_level=False) 
+1
source share

All Articles