使用dataframe的 sort_values 多列排序,全设置降序排列
import pandas as pd
medal = {
"金": [8, 8, 7, 10],
"银": [3, 7, 5, 7],
"铜": [8, 3, 7, 3],
"总数": [19, 18, 19, 20],
}
df = pd.DataFrame(medal)
rank = df.sort_values(by=["金", "银", "铜", "总数"], ascending=[False, False, False, False])
print(rank)
输出结果:
金 银 铜 总数
3 10 7 3 20
1 8 7 3 18
0 8 3 8 19
2 7 5 7 19