Pandas:基于多列对数据框进行排序 [英] Pandas: Sort a dataframe based on multiple columns
问题描述
我知道这个问题已经被问过好几次了.但没有一个答案符合我的情况.
I know that this question has been asked several times. But none of the answers match my case.
我有一个包含列、部门和员工计数的 Pandas 数据框.我需要按降序对employee_count 列进行排序.但如果 2 个 employee_counts 之间存在平局,则应根据部门的字母顺序对它们进行排序.
I've a pandas dataframe with columns,department and employee_count. I need to sort the employee_count column in descending order. But if there is a tie between 2 employee_counts then they should be sorted alphabetically based on department.
Department Employee_Count
0 abc 10
1 adc 10
2 bca 11
3 cde 9
4 xyz 15
required output:
Department Employee_Count
0 xyz 15
1 bca 11
2 abc 10
3 adc 10
4 cde 9
这是我试过的.
df = df.sort_values(['Department','Employee_Count'],ascending=[True,False])
但这只是按字母顺序对部门进行排序.
But this just sorts the departments alphabetically.
我还尝试先按部门排序,然后按 Employee_Count 排序.像这样:
I've also tried to sort by Department first and then by Employee_Count. Like this:
df = df.sort_values(['Department'],ascending=[True])
df = df.sort_values(['Employee_Count'],ascending=[False])
这也没有给我正确的输出:
This doesn't give me correct output either:
Department Employee_Count
4 xyz 15
2 bca 11
1 adc 10
0 abc 10
3 cde 9
它首先给出adc",然后给出abc".请帮助我.
It gives 'adc' first and then 'abc'. Kindly help me.
推荐答案
您可以交换列表中的列以及ascending
参数中的值:
You can swap columns in list and also values in ascending
parameter:
说明:
列名的顺序是排序的顺序,首先按Employee_Count
降序排序,如果Employee_Count
中有重复,则按Department
排序只重复行升序.
Order of columns names is order of sorting, first sort descending by Employee_Count
and if some duplicates in Employee_Count
then sorting by Department
only duplicates rows ascending.
df1 = df.sort_values(['Employee_Count', 'Department'], ascending=[False, True])
print (df1)
Department Employee_Count
4 xyz 15
2 bca 11
0 abc 10 <-
1 adc 10 <-
3 cde 9
或者为了测试是否使用第二个 False
然后重复的行正在排序 descending
:
Or for test if use second False
then duplicated rows are sorting descending
:
df2 = df.sort_values(['Employee_Count', 'Department',],ascending=[False, False])
print (df2)
Department Employee_Count
4 xyz 15
2 bca 11
1 adc 10 <-
0 abc 10 <-
3 cde 9
这篇关于Pandas:基于多列对数据框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!