python - Pandas groupby object filtering -
i have pandas dataframe
df.columns index([u’car_id’,u’color’,u’make’,u’year’)]
i create new filterable object has count of each group (color,make,year);
grp = df[[‘color’,’make’,’year’]].groupby([‘color’,’make’,’year’]).size()
which return this
color make year count black honda 2011 416
i able filter it, when try this:
grp.filter(lambda x: x[‘color’]==‘black’)
i receive error
typeerror: 'function' object not iterable
how leverage 'groupby' object in order filter rows out?
i think need add reset_index
, output dataframe
. last use boolean indexing
:
df = df[['color','make','year']].groupby(['color','make','year']) .size() .reset_index(name='count') df1 = df[df.color == 'black']
Comments
Post a Comment