pandas AttributeError:无法访问“DataFrameGroupBy”对象的可调用属性“reset_index”,请尝试使用“apply”方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50465023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:35:17  来源:igfitidea点击:

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

python-3.xpandaspandas-groupby

提问by Gingerbread

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

我对Pandas很陌生,并试图使用groupby. 我有一个多列的 df。

  • I want to groupby a particular column and then sort each group based on a different column.
  • I want to groupby col1and then sort each group by col5and then do reset_indexto 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分组,然后对每个组进行排序col5,然后执行reset_index以获取数据帧的所有行。
  • 我收到以下错误 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()

回答by jezrael

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

对于groupby一些需要聚合功能(S),像meansummax

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

Or:

或者:

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

回答by yogitha jaya reddy gari

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?

请参阅此 stackoverflow 链接以通过示例如何清楚地说明 如何一步重置所有组的 DataFrame 索引?

回答by hakuna_code

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']))