Sort a Pandas Series in Python - GeeksforGeeks

05 Jul, 2021
Series is a one-dimensional labeled array capable of holding data of the type integer, string, float, python objects, etc. The axis labels are collectively called index. 
Now, Let’s see a program to sort a Pandas Series.
For sorting a pandas series the Series.sort_values() method is used.
Syntax: Series.sort_values(axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)Sorted
Returns: Sorted series
Examples 1: Sorting a numeric series in ascending order.
Python3
# importing pandas as pdimport pandas as pd # define a numeric seriess = pd.Series([100, 200, 54.67,               300.12, 400]) # print the unsorted seriess
 
 
Output:
 
 
Now we will use Series.sort_values() method to sort a numeric series in ascending order.
 
Python3
# sorting series s with# s.sort_value() method in# ascending ordersorted_series = s.sort_values(ascending                              = True)# print the sorted seriessorted_series
Output:
From the output, we can see that the numeric series is sorted in ascending order.
Example 2:  Sorting a numeric series in descending order.
Python3
# importing pandas as pdimport pandas as pd # define a numeric seriess = pd.Series([100, 200, 54.67,               300.12, 400]) # print the unsorted seriess
 
 
Output:
 
 
Now we will use Series.sort_values() method to sort a numeric series in descending order.
 
Python3
# sorting the series s with# s.sort_values() method# in descending ordersorted_series = s.sort_values(ascending                              = False)# printing the sorted seriessorted_series
Output:
From the output, we can see that the numeric series is sorted in descending order.
Example 3: Sorting a series of strings.
Python3
# importing pandas as pdimport pandas as pd #d efine a string series ss = pd.Series(["OS","DBMS","DAA",               "TOC","ML"]) # print the unsorted seriess
Output:
Now we will use Series.sort_values() method to sort a series of strings.
Python3
# sorting the series s with# s.sort_values() method# in ascending ordersorted_series = s.sort_values(ascending                              = True)# printing the sorted seriessorted_series
Output:
From the output, we can see that the string series is sorted in a lexicographically ascending order.
Example 4: Sorting values inplace.
Python3
# importing numpy as npimport numpy as np # importing pandas as pdimport pandas as pd # define a numeric series# s with a NaNs = pd.Series([np.nan, 1, 3,               10, 5]) # print the unsorted seriess
Output:
Now we will use Series.sort_values() method to sort values inplace
Python3
# sorting the series s with# s.sort_values() method in# descending order and inplaces.sort_values(ascending  = False,                              inplace = True) # printing the sorted seriess
 
 
Output:
 
 
The output shows that the inplace sorting in the Pandas Series.
 
Example 5: Sorting values in the series by putting NaN first.
 
Python3
# importing numpy as npimport numpy as np # importing pandas as pdimport pandas as pd  # define a numeric series# s with a NaNs = pd.Series([np.nan, 1, 3,               10, 5]) # print the unsorted seriess
 
 
Output: 
 
 
Now we will use Series.sort_values() method to sort values in the series by putting NaN first.
 
Python3
# sorting the series s with# s.sort_values() method in# ascending order with na# position at firstsorted_series = s.sort_values(na_position =                              'first') # printing the sorted seriessorted_series
 
 
Output:
 
 
The output shows that the NaN (not a number) value is given the first place in the sorted series.
 
sweetyty
Python pandas-series
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists