首页 > 解决方案 > AttributeError:无法访问“DataFrameGroupBy”对象的可调用属性“reset_index”,请尝试使用“应用”方法

问题描述

我对 pandas 很陌生,并尝试使用groupby. 我有一个包含多列的 df。

我的输入数据框:

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()

标签: python-3.xpandaspandas-groupby

解决方案


对于groupby需要一些聚合函数,如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()

推荐阅读