I am very new to pandas and trying to use groupby
. I have a df with multiple columns.
col1
and then sort each group by col5
and then do reset_index
to get all rows of the dataframe.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()
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()
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']))
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With