Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method

I am very new to pandas and trying to use groupby. I have a df with multiple columns.

  • I want to groupby a particular column and then sort each group based on a different column.
  • I want to groupby col1 and then sort each group by col5 and then do reset_index to get all rows of the dataframe.
  • I get the following error AttributeError: Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method.

My input dataframe:

col1 |  col2 | col3 | col4 | col5
=================================
A    |   A1   | A2   | A3   | DATE1
A    |   B1   | B2   | B3   | DATE2

My code:

df.sort_values(['col5'],ascending=False).groupby('col1').reset_index()
like image 295
Gingerbread Avatar asked May 22 '18 10:05

Gingerbread


3 Answers

For groupby need some aggregation function(s), like mean, sum, max:

df.sort_values(['col5'],ascending=False).groupby('col1').mean().reset_index()

Or:

df.sort_values(['col5'],ascending=False).groupby('col1', as_index=False).mean()
like image 118
jezrael Avatar answered Nov 18 '22 14:11

jezrael


You can try the below code, I had a similar issue.

grouped=data.groupby(['Colname'])
grouped.apply(lambda _df: _df.sort_values(by=['col_to_be_sorted']))

like image 27
hakuna_code Avatar answered Nov 18 '22 14:11

hakuna_code


you can use

grouped = df.sort_values(['col5'],ascending=False).groupby('col1',as_index = False).apply(lambda x: x.reset_index(drop = True))
grouped.reset_index().drop(['level_0','level_1'],axis = 1)

Refer to this stackoverflow link for clear explanation with an example How to reset a DataFrame's indexes for all groups in one step?

like image 45
yogitha jaya reddy gari Avatar answered Nov 18 '22 16:11

yogitha jaya reddy gari