有一个类似的问题here,但并不是我要找的东西。
我想根据字典对数据框进行排序,该字典指定要排序的列以及每一列的顺序。
示例:
df =
+-------+-------+-----------+------+
| Index | Time | Month | Year |
+-------+-------+-----------+------+
| 0 | 13:00 | January | 2018 |
| 1 | 14:30 | March | 2015 |
| 2 | 12:00 | November | 2003 |
| 3 | 10:15 | September | 2012 |
| 4 | 13:30 | October | 2012 |
| 5 | 06:25 | June | 2012 |
| 6 | 07:50 | August | 2019 |
| 7 | 09:20 | May | 2015 |
| 8 | 22:30 | July | 2016 |
| 9 | 23:05 | April | 2013 |
| 10 | 21:10 | April | 2008 |
+-------+-------+-----------+------+
sort_dict = {'Month': 'Ascending', 'Year': 'Descending', 'Time': 'Ascending'}
df.sort_values(by=sort_dict)
df =
+-------+-------+-----------+------+
| Index | Time | Month | Year |
+-------+-------+-----------+------+
| 0 | 13:00 | January | 2018 |
| 1 | 14:30 | March | 2015 |
| 9 | 23:05 | April | 2013 |
| 10 | 21:10 | April | 2008 |
| 7 | 09:20 | May | 2015 |
| 5 | 06:25 | June | 2012 |
| 8 | 22:30 | July | 2016 |
| 6 | 07:50 | August | 2019 |
| 3 | 10:15 | September | 2012 |
| 4 | 13:30 | October | 2012 |
| 2 | 12:00 | November | 2003 |
+-------+-------+-----------+------+
感谢您的帮助!
列索引也可以:
sort_dict = {2: 'Ascending', 3: 'Descending', 1: 'Ascending'}
答案 0 :(得分:2)
编辑:(感谢@Jon Clements)
在python 3.6中,声明sort_dict
的键顺序将按照指定的顺序进行,但是在3.6之前,字典文字不一定会保留顺序。例如。在3.5中,声明sort_dict最终为{'Month': 'Ascending', 'Time': 'Ascending', 'Year': 'Descending'}
...,这将是不同的迭代顺序-因此将产生不同的排序结果。
如果需要始终可以使用相同的顺序,则可以使用构造函数使用OrderedDict
或Series
-顺序不取决于python版本。
一种可能的解决方案是创建助手Series
,然后将索引转换为列表,并同时传递参数ascending
填充的布尔列表:
s = pd.Series(sort_dict)
print (s)
Month Ascending
Year Descending
Time Ascending
dtype: object
df = df.sort_values(by=s.index.tolist(), ascending = (s == 'Ascending'))
print (df)
Time Month Year
Index
9 23:05 April 2013
10 21:10 April 2008
6 07:50 August 2019
0 13:00 January 2018
8 22:30 July 2016
5 06:25 June 2012
1 14:30 March 2015
7 09:20 May 2015
2 12:00 November 2003
4 13:30 October 2012
3 10:15 September 2012