How to Sort Pandas DataFrame? - GeeksforGeeks

28 Jul, 2020
In this article, we will discuss how to sort Pandas Dataframe. Let’s create a dataframe.
Example :
Python3
# importing pandas libraryimport pandas as pd  # creating and initializing a nested listage_list = [['Afghanistan', 1952, 8425333, 'Asia'],            ['Australia', 1957, 9712569, 'Oceania'],            ['Brazil', 1962, 76039390, 'Americas'],            ['China', 1957, 637408000, 'Asia'],            ['France', 1957, 44310863, 'Europe'],            ['India', 1952, 3.72e+08, 'Asia'],            ['United States', 1957, 171984000, 'Americas']]  # creating a pandas dataframedf = pd.DataFrame(age_list, columns=['Country', 'Year',                                     'Population', 'Continent'])  df
Output :
In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.
Example 1: Sorting the Data frame in Ascending order 
Python3
# importing pandas libraryimport pandas as pd  # creating and initializing a nested list age_list = [['Afghanistan',1952,8425333,'Asia'],            ['Australia',1957,9712569,'Oceania'],            ['Brazil',1962,76039390,'Americas'],            ['China',1957,637408000,'Asia'],            ['France',1957,44310863,'Europe'],            ['India',1952,3.72e+08,'Asia'],            ['United States',1957,171984000,'Americas']]  # creating a pandas dataframedf = pd.DataFrame(age_list,columns=['Country','Year',                                    'Population','Continent'])  # Sorting by column 'Country'df.sort_values(by=['Country'])
Output :
Example 2: Sorting the Data frame in Descending order 
Python3
# Sorting Pandas Dataframe in Descending Order  # importing pandas libraryimport pandas as pd  # Initializing the nested list with Data setage_list = [['Afghanistan', 1952, 8425333, 'Asia'],            ['Australia', 1957, 9712569, 'Oceania'],            ['Brazil', 1962, 76039390, 'Americas'],            ['China', 1957, 637408000, 'Asia'],            ['France', 1957, 44310863, 'Europe'],            ['India', 1952, 3.72e+08, 'Asia'],            ['United States', 1957, 171984000, 'Americas']]  # creating a pandas dataframedf = pd.DataFrame(age_list, columns=['Country', 'Year',                                     'Population', 'Continent'])  # Sorting by column "Population"df.sort_values(by=['Population'], ascending=False)
Output :
Example 3: Sorting Pandas Data frame by putting missing values first
Python3
# Sorting Pandas Data frame by putting# missing values first  # importing pandas libraryimport pandas as pd  # Initializing the nested list with Data setage_list = [['Afghanistan', 1952, 8425333, 'Asia'],            ['Australia', 1957, 9712569, 'Oceania'],            ['Brazil', 1962, 76039390, 'Americas'],            ['China', 1957, 637408000, 'Asia'],            ['France', 1957, 44310863, 'Europe'],            ['India', 1952, 3.72e+08, 'Asia'],            ['United States', 1957, 0, 'Americas']]  # creating a pandas dataframedf = pd.DataFrame(age_list, columns=['Country', 'Year',                                     'Population', 'Continent'])  # Sorting by column "Population"# by putting missing values firstdf.sort_values(by=['Population'], na_position='first')
Output :
Example 4:  Sorting Data frames by multiple columns
Python3
#  Sorting Pandas Dataframe based on# the Values of Multiple Columns  # importing pandas libraryimport pandas as pd  # Initializing the nested list with data setage_list = [['Afghanistan', 1952, 8425333, 'Asia'],            ['Australia', 1957, 9712569, 'Oceania'],            ['Brazil', 1962, 76039390, 'Americas'],            ['China', 1957, 637408000, 'Asia'],            ['France', 1957, 44310863, 'Europe'],            ['India', 1952, 3.72e+08, 'Asia'],            ['United States', 1957, 171984000, 'Americas']]  # creating a pandas dataframedf = pd.DataFrame(age_list, columns=['Country', 'Year',                                     'Population', 'Continent'])    # Sorting by columns "Country" and then "Continent"df.sort_values(by=['Country', 'Continent'])
Output :
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()