Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working With Series in Reverse Order (Latest First)

Tags:

pandas

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

like image 398
DonQuixote Avatar asked May 06 '13 16:05

DonQuixote


People also ask

How do you reverse a series order in python?

A Python list has a reverse function. The [::-1] slice operation to reverse a Python sequence. The reversed built-in function returns a reverse iterator. The object's __reversed__ magic method is called by the reversed built-in to implement reverse iteration.

How do you reverse a series in pandas?

With the help of Pandas, we can perform a reverse operation by using loc(), iloc(), reindex(), slicing, and indexing on a row of a data set.

How do you sort Series in descending?

4. Sort Series in Descending Order. To sort the above series in descending order, use the sort_values() function with ascending=False .


1 Answers

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
like image 123
Wouter Overmeire Avatar answered Oct 09 '22 13:10

Wouter Overmeire