pandas 以相反的顺序处理系列(最新在前)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16403442/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:48:25  来源:igfitidea点击:

Working With Series in Reverse Order (Latest First)

pandas

提问by DonQuixote

How can I reverse sort order of DataSeries in Pandas so that I'm working with them in descending order?

如何反转 Pandas 中 DataSeries 的排序顺序,以便我按降序使用它们?

回答by Wouter Overmeire

In [28]: s = pd.Series([20, 10, 30], ['c', 'a', 'b'])

In [29]: s
Out[29]:
c    20
a    10
b    30
dtype: int64

Sorting on the index

按索引排序

In [30]: s.sort_index(ascending=False)
Out[30]:
c    20
b    30
a    10
dtype: int64

sorting on the values

对值进行排序

In [31]: s.sort()

In [32]: s[::-1]
Out[32]:
b    30
c    20
a    10
dtype: int64