Group by index + column in pandas -
i have dataframe has columns
- user_id
- item_bought
here user_id index of df. want group both user_id , item_bought , item wise count user. how do that.
thanks
this should work:
>>> df = pd.dataframe(np.random.randint(0,5,(6, 2)), columns=['col1','col2']) >>> df['ind1'] = list('aaabcc') >>> df['ind2'] = range(6) >>> df.set_index(['ind1','ind2'], inplace=true) >>> df col1 col2 ind1 ind2 0 3 2 1 2 0 2 2 3 b 3 2 4 c 4 3 1 5 0 0 >>> df.groupby([df.index.get_level_values(0),'col1']).count() col2 ind1 col1 2 2 3 1 b 2 1 c 0 1 3 1
i had same problem using 1 of columns multiindex. multiindex, cannot use df.index.levels[0] since has distinct values particular index level , of different size whole dataframe...
check http://pandas.pydata.org/pandas-docs/stable/generated/pandas.index.get_level_values.html - get_level_values "return vector of label values requested level, equal length of index"
Comments
Post a Comment