dataframe 按照指定顺序排序
Posted ~小疯子~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dataframe 按照指定顺序排序相关的知识,希望对你有一定的参考价值。
如下index 按照区间排序 ['0_5', '6_9','10_14', '15_19', '20_29', '30_49', '50_99', '100_199', '200_299', '300_399','400_1000']
a | b | c | d | e | f | g | h | |
---|---|---|---|---|---|---|---|---|
0_5 | 0.05836 | 0.27791 | 0.38913 | 0.05282 | 0.04625 | 0.00162 | 0.02343 | 0.06217 |
100_199 | 0.05687 | 0.26901 | 0.36438 | 0.06024 | 0.04972 | 0.00149 | 0.02946 | 0.07987 |
10_14 | 0.05547 | 0.27348 | 0.39620 | 0.05252 | 0.04469 | 0.00169 | 0.02413 | 0.06581 |
15_19 | 0.05698 | 0.27087 | 0.39804 | 0.05251 | 0.04417 | 0.00133 | 0.02324 | 0.06679 |
200_299 | 0.05840 | 0.25521 | 0.35803 | 0.06378 | 0.05252 | 0.00167 | 0.03152 | 0.08889 |
20_29 | 0.05544 | 0.27226 | 0.39479 | 0.05264 | 0.04486 | 0.00164 | 0.02417 | 0.06892 |
300_399 | 0.05979 | 0.24189 | 0.35784 | 0.06597 | 0.05365 | 0.00151 | 0.03069 | 0.09904 |
30_49 | 0.05654 | 0.27353 | 0.38634 | 0.05340 | 0.04555 | 0.00157 | 0.02436 | 0.06994 |
400_1000 | 0.05601 | 0.23132 | 0.35805 | 0.06763 | 0.05734 | 0.00144 | 0.03205 | 0.10433 |
50_99 | 0.05757 | 0.27437 | 0.37233 | 0.05603 | 0.04771 | 0.00162 | 0.02654 | 0.07371 |
6_9 | 0.05533 | 0.27438 | 0.39855 | 0.05237 | 0.04536 | 0.00177 | 0.02272 | 0.06442 |
一行代码就可以了, 如果要在列中对某一列排序,可以把那一列设置为index , 然后在排序, 很方便
sort_list = ['0_5', '6_9','10_14', '15_19', '20_29', '30_49', '50_99', '100_199', '200_299', '300_399','400_1000']
dd = dd.loc[sort_list]
a | b | c | d | e | f | g | h | |
---|---|---|---|---|---|---|---|---|
0_5 | 0.05836 | 0.27791 | 0.38913 | 0.05282 | 0.04625 | 0.00162 | 0.02343 | 0.06217 |
6_9 | 0.05533 | 0.27438 | 0.39855 | 0.05237 | 0.04536 | 0.00177 | 0.02272 | 0.06442 |
10_14 | 0.05547 | 0.27348 | 0.39620 | 0.05252 | 0.04469 | 0.00169 | 0.02413 | 0.06581 |
15_19 | 0.05698 | 0.27087 | 0.39804 | 0.05251 | 0.04417 | 0.00133 | 0.02324 | 0.06679 |
20_29 | 0.05544 | 0.27226 | 0.39479 | 0.05264 | 0.04486 | 0.00164 | 0.02417 | 0.06892 |
30_49 | 0.05654 | 0.27353 | 0.38634 | 0.05340 | 0.04555 | 0.00157 | 0.02436 | 0.06994 |
50_99 | 0.05757 | 0.27437 | 0.37233 | 0.05603 | 0.04771 | 0.00162 | 0.02654 | 0.07371 |
100_199 | 0.05687 | 0.26901 | 0.36438 | 0.06024 | 0.04972 | 0.00149 | 0.02946 | 0.07987 |
200_299 | 0.05840 | 0.25521 | 0.35803 | 0.06378 | 0.05252 | 0.00167 | 0.03152 | 0.08889 |
300_399 | 0.05979 | 0.24189 | 0.35784 | 0.06597 | 0.05365 | 0.00151 | 0.03069 | 0.09904 |
400_1000 | 0.05601 | 0.23132 | 0.35805 | 0.06763 | 0.05734 | 0.00144 | 0.03205 | 0.10433 |
以上是关于dataframe 按照指定顺序排序的主要内容,如果未能解决你的问题,请参考以下文章
pandas使用sort_values函数将dataframe按照指定数据列的内容对dataframe的数据行进行排序(sort dataframe rows by a specific column