我对熊猫很陌生,正在尝试使用 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()
原文由 Gingerbread 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于
groupby
sum
一些聚合功能(S),例如mean
max
或者: