我有一个这样的数据框:
innings batsman batsman_runs
1 AA Noffke 9
1 B Akhil 0
1 BB McCullum 158
1 CL White 6
1 DJ Hussey 12....
我需要按降序对batsman_runs进行排序并显示相应的击球手,即如果158位于顶部,那么BB McCullum应该在相邻的单元格中。我尝试了以下代码:
df['batsman_runs'].sort_values(ascending=False)
但是这段代码没有显示击球手的名字。如何用相应的击球手显示跑步?
答案 0 :(得分:5)
试试这个:
In [32]: df.sort_values('batsman_runs', ascending=0)
Out[32]:
innings batsman batsman_runs
2 1 BB McCullum 158
4 1 DJ Hussey 12
0 1 AA Noffke 9
3 1 CL White 6
1 1 B Akhil 0
或:
In [31]: df.sort_values('batsman', ascending=0)
Out[31]:
innings batsman batsman_runs
4 1 DJ Hussey 12
3 1 CL White 6
2 1 BB McCullum 158
1 1 B Akhil 0
0 1 AA Noffke 9
您还可以组合多个列:
In [34]: df.sort_values(['batsman','batsman_runs'], ascending=[1,0])
Out[34]:
innings batsman batsman_runs
0 1 AA Noffke 9
1 1 B Akhil 0
2 1 BB McCullum 158
3 1 CL White 6
4 1 DJ Hussey 12