pandas 基于列表对熊猫数据框进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26707171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sort pandas dataframe based on list
提问by user308827
I would like to sort the following dataframe:
我想对以下数据框进行排序:
Region LSE North South
0 Cn 33.330367 9.178917
1 Develd -36.157025 -27.669988
2 Wetnds -38.480206 -46.089908
3 Oands -47.986764 -32.324991
4 Otherg 323.209834 28.486310
5 Soys 34.936147 4.072872
6 Wht 0.983977 -14.972555
I would like to sort it so the LSE column is reordered based on the list:
我想对其进行排序,以便根据列表重新排序 LSE 列:
lst = ['Oands','Wetnds','Develd','Cn','Soys','Otherg','Wht']
of, course the other columns will need to be reordered accordingly as well. Is there any way to do this in pandas?
当然,其他列也需要相应地重新排序。有没有办法在Pandas中做到这一点?
回答by Marius
The improved support for Categoricals in pandas version 0.15 allows you to do this easily:
Categoricalpandas 0.15 版中对s的改进支持使您可以轻松地做到这一点:
df['LSE_cat'] = pd.Categorical(
df['LSE'],
categories=['Oands','Wetnds','Develd','Cn','Soys','Otherg','Wht'],
ordered=True
)
df.sort('LSE_cat')
Out[5]:
Region LSE North South LSE_cat
3 3 Oands -47.986764 -32.324991 Oands
2 2 Wetnds -38.480206 -46.089908 Wetnds
1 1 Develd -36.157025 -27.669988 Develd
0 0 Cn 33.330367 9.178917 Cn
5 5 Soys 34.936147 4.072872 Soys
4 4 Otherg 323.209834 28.486310 Otherg
6 6 Wht 0.983977 -14.972555 Wht
If this is only a temporary ordering then keeping the LSE column as
a Categoricalmay not be what you want, but if this ordering is
something that you want to be able to make use of a few times
in different contexts, Categoricalsare a great solution.
如果这只是一个临时排序,那么将 LSE 列保留为 aCategorical可能不是您想要的,但如果您希望能够在不同上下文中多次使用此排序,这Categoricals是一个很好的解决方案。
In later versions of pandas, sort, has been replaced with sort_values, so you would need instead:
在 , 的更高版本中pandas,sort已被替换为sort_values,因此您需要:
df.sort_values('LSE_cat')

