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

时间:2018-05-22 10:08:10

标签: python-3.x pandas pandas-groupby

我对熊猫很新,并尝试使用groupby。我有一个有多列的df。我想分组一个特定的列,然后根据不同的列对每个组进行排序。我收到以下错误 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

我想通过col1进行分组,然后按col5对每个组进行排序,然后执行reset_index以获取数据帧的所有行。

以下是我使用的代码。 df.sort_values(['col5'],ascending=False).groupby('col1').reset_index()

3 个答案:

答案 0 :(得分:0)

对于groupby需要一些聚合函数,例如meansummax

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

或者:

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

答案 1 :(得分:0)

您可以使用

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)

请参考此stackoverflow链接以获取示例的清晰说明 How to reset a DataFrame's indexes for all groups in one step?

答案 2 :(得分:0)

您可以尝试以下代码,我遇到了类似的问题。

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