How to Sort Pandas DataFrame?

In this article, we will discuss how to sort Pandas Dataframe. Let’s create a dataframe.

Example :

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas library
import 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 dataframe
df = pd.DataFrame(age_list, columns=['Country', 'Year',
                                     'Population', 'Continent'])
  
df

chevron_right


Output :



Sorting Pandas Data Frame

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas library
import 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 dataframe
df = pd.DataFrame(age_list,columns=['Country','Year',
                                    'Population','Continent'])
  
# Sorting by column 'Country'
df.sort_values(by=['Country'])

chevron_right


Output :

Example 2: Sorting the Data frame in Descending order 

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# Sorting Pandas Dataframe in Descending Order
  
# importing pandas library
import pandas as pd
  
# Initializing the nested list with Data set
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 dataframe
df = pd.DataFrame(age_list, columns=['Country', 'Year',
                                     'Population', 'Continent'])
  
# Sorting by column "Population"
df.sort_values(by=['Population'], ascending=False)

chevron_right


Output :

Example 3: Sorting Pandas Data frame by putting missing values first

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Sorting Pandas Data frame by putting
# missing values first
  
# importing pandas library
import pandas as pd
  
# Initializing the nested list with Data set
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, 0, 'Americas']]
  
# creating a pandas dataframe
df = pd.DataFrame(age_list, columns=['Country', 'Year',
                                     'Population', 'Continent'])
  
# Sorting by column "Population"
# by putting missing values first
df.sort_values(by=['Population'], na_position='first')

chevron_right


Output :

Example 4:  Sorting Data frames by multiple columns

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

#  Sorting Pandas Dataframe based on
# the Values of Multiple Columns
  
# importing pandas library
import pandas as pd
  
# Initializing the nested list with data set
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 dataframe
df = pd.DataFrame(age_list, columns=['Country', 'Year',
                                     'Population', 'Continent'])
  
  
# Sorting by columns "Country" and then "Continent"
df.sort_values(by=['Country', 'Continent'])

chevron_right


Output :




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.