AttributeError:无法访问"DataFrameGroupBy"对象的可调用属性"reset_index",请尝试使用"apply"方法 [英] AttributeError: Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method
本文介绍了AttributeError:无法访问"DataFrameGroupBy"对象的可调用属性"reset_index",请尝试使用"apply"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对熊猫很陌生,尝试使用groupby
.我有一个包含多列的df.
I am very new to pandas and trying to use groupby
. I have a df with multiple columns.
- 我想对特定列进行分组,然后根据不同的列对每个组进行排序.
- 我想对
col1
进行分组,然后按col5
对每个组进行排序,然后执行reset_index
以获得数据帧的所有行. - 我收到以下错误
AttributeError: Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method
.
- 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 bycol5
and then doreset_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
.
我的输入数据框:
col1 | col2 | col3 | col4 | col5
=================================
A | A1 | A2 | A3 | DATE1
A | B1 | B2 | B3 | DATE2
我的代码:
df.sort_values(['col5'],ascending=False).groupby('col1').reset_index()
推荐答案
对于groupby
,需要一些聚合函数,例如mean
,sum
,max
:
For groupby
need some aggregation function(s), like mean
, sum
, max
:
df.sort_values(['col5'],ascending=False).groupby('col1').mean().reset_index()
或者:
df.sort_values(['col5'],ascending=False).groupby('col1', as_index=False).mean()
这篇关于AttributeError:无法访问"DataFrameGroupBy"对象的可调用属性"reset_index",请尝试使用"apply"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文