【发布时间】:2018-11-01 01:33:45
【问题描述】:
我对 pandas 很陌生,正在尝试使用 groupby。我有一个包含多列的 df。
- 我想按特定列进行分组,然后根据不同的列对每个组进行排序。
- 我想按
col1分组,然后按col5对每个组进行排序,然后执行reset_index以获取数据框的所有行。 - 我收到以下错误
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代码是什么? -
提供样本输出
-
@jezrael:我已经提供了示例代码。
标签: python-3.x pandas pandas-groupby