【发布时间】:2021-08-27 09:55:32
【问题描述】:
创建的df:
import pandas as pd
data = {'Type': ['A', 'B', 'C', 'C', 'A', 'B'],
'Name': ['ab', 'bc', 'Cd', 'ef', 'gh', 'ij'],
'Ratings': [20, 21, 19, 18, 10, 5]}
# Create DataFrame
df = pd.DataFrame(data)
pivot = df.pivot_table(index=['Type', 'Name', 'Ratings'], aggfunc='sum')
pivot.sort_values(['Type', 'Ratings'], ascending=False)
df3 = pivot.reset_index().sort_values(['Type', 'Ratings'], ascending=[True, False]).set_index(['Type', 'Name'])
print(df3)
输出:
Type Name Ratings
A ab 20
gh 10
B bc 21
ij 5
C Cd 19
ef 18
需要的输出:
Type Name Ratings
C Cd 19
ef 18
A ab 20
gh 10
B bc 21
ij 5
输出:
Ratings
Type Name Ratings
A ab 20
gh 10
B bc 21
ij 5
C Cd 19
ef 18
需要的输出:
Type Name Ratings
C Cd 19
ef 18
A ab 20
gh 10
B bc 21
ij 5
预期:由于 c 的总评分为 37,a:30,B:26`
【问题讨论】:
标签: pandas sorting pivot-table