How to Sort Pandas DataFrame (with examples)

To sort pandas DataFrame, you may use the df.sort_values syntax in Python.

In this short tutorial, I’ll show you 4 examples to demonstrate how to sort:

  1. column in an ascending order
  2. column in an descending order
  3. by multiple columns – case#1
  4. by multiple columns – case#2

To start with a simple example, let’s say that you have the following ‘cars’ data:

 

BrandPriceYear
Honda Civic220002015
Toyota Corolla250002013
Ford Focus270002018
Audi A4350002018

 

In order to capture that data in Python, you decided to create the following pandas DataFrame:

 

from pandas import DataFrame
 
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000],
        'Year': [2015,2013,2018,2018]
        }
 
df = DataFrame(Cars, columns= ['Brand', 'Price','Year'])

print (df)

 

And when you run the Python code above, you’ll get:

DataFrame Python

Next, I’m going to show you how to sort that DataFrame using 4 different examples.

Example 1: Sort Pandas DataFrame in an ascending order

Let’s say that you want to sort the DataFrame, such that the Brand will be displayed in an ascending order. In that case, you’ll need to add the following portion to the code:

 

df.sort_values(by=['Brand'], inplace=True)

 

Note that unless specified otherwise, the values will be sorted in an ascending order by default.

The full python code would look like this:

 

# sort - ascending order
from pandas import DataFrame
 
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000],
        'Year': [2015,2013,2018,2018]
        }
 
df = DataFrame(Cars, columns= ['Brand', 'Price','Year'])

# sort Brand - ascending order
df.sort_values(by=['Brand'], inplace=True)

print (df)

 

When you run the code, you’ll notice that the brand will indeed get sorted in an ascending order, where Audi A4 would be the first record, while Toyota Corolla would be the last:

How to Sort Pandas DataFrame (with examples)

Example 2: Sort Pandas DataFrame in a descending order

Alternatively, you can sort the Brand column in a descending order. To do that, simply add the condition of ascending=False in this manner:

 

df.sort_values(by=['Brand'], inplace=True, ascending=False)

 

And the full Python code would be:

 

# sort - descending order
from pandas import DataFrame
 
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000],
        'Year': [2015,2013,2018,2018]
        }
 
df = DataFrame(Cars, columns= ['Brand', 'Price','Year'])

# sort Brand - descending order
df.sort_values(by=['Brand'], inplace=True, ascending=False)

print (df)

 

You’ll now notice that Toyota Corolla would be the first record, while Audi A4 would be the last (as you would expect to get when applying a descending order for our sample):

How to Sort Pandas DataFrame

Example 3: Sort by multiple columns – case#1

But what if you want to sort by multiple columns?

In that case, you may use the following structure to sort by multiple columns:

 

df.sort_values(by=['First Column','Second Column',...], inplace=True)

 

Suppose that you want to sort by both ‘Year’ and ‘Price.’ Since we have two records where the Year is 2018 (i.e., for the Ford Focus and Audi A4), then sorting by a second column – the ‘Price’ column –  would become useful:

 

 df.sort_values(by=['Year','Price'], inplace=True)

 

Here is the Python code that you may use:

 

# sort by multiple columns
from pandas import DataFrame
 
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000],
        'Year': [2015,2013,2018,2018]
        }
 
df = DataFrame(Cars, columns= ['Brand', 'Price','Year'])

# sort by multiple columns: Year and Price
df.sort_values(by=['Year','Price'], inplace=True)

print (df)

 

Notice that all the records are sorted by both the year and price in an ascending order, so Ford Focus would appear before Audi A4:

Sort Pandas DataFrame

Also note that the ‘Year’ column takes the priority when performing the sorting, as it was placed in the df.sort_values statement prior to the ‘Price’ column.

Example 4: Sort by multiple columns – case#2

Finally, let’s sort by the columns of ‘Year’ and ‘Brand’ as follows:

 

df.sort_values(by=['Year','Brand'], inplace=True)

 

And the complete Python code:

 

# sort by multiple columns
from pandas import DataFrame
 
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4'],
        'Price': [22000,25000,27000,35000],
        'Year': [2015,2013,2018,2018]
        }
 
df = DataFrame(Cars, columns= ['Brand', 'Price','Year'])

# sort by multiple columns: Year and Brand
df.sort_values(by=['Year','Brand'], inplace=True)

print (df)

 

You’ll now see that all the records are sorted by both the year and brand in an ascending order, so this time Audi A4 would appear prior to Ford Focus:

How to Sort Pandas DataFrame (with examples)

You may want to check the pandas documentation to learn more about sorting values in pandas DataFrame.