假设我有这个数据框:
df = pd.DataFrame({
'price': [2, 13, 24, 15, 11, 44],
'category': ["shirts", "pants", "shirts", "tops", "hat", "tops"],
})
price category
0 2 shirts
1 13 pants
2 24 shirts
3 15 tops
4 11 hat
5 44 tops
我希望能够按照以下方式对数值进行排序:
- 找到每个类别中的最高价格。
- 根据最高价格对类别进行排序(在此情况下,按降序排列:上衣、衬衫、裤子、帽子)。
- 按较高价格对每个类别进行排序。
最终数据框应如下所示:
price category
0 44 tops
1 15 tops
2 24 shirts
3 24 shirts
4 13 pants
5 11 hat