How can I reverse sort order of DataSeries in Pandas so that I'm working with them in descending order?
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.
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.
4. Sort Series in Descending Order. To sort the above series in descending order, use the sort_values() function with ascending=False .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With