How to Sort DataFrame by Column in Pandas?

Sorting data is essential for organizing large amounts of data. Sorted data will be easier to understand, easy to analyze, and easy to use for further research or operations. The Python library named “Pandas” is used to analyze various data types. A fast and efficient analysis tool is provided by this library to its users. A 2-Dimensioned data structure named “DataFrame” organizes data in the labeled form of rows and columns. In Python, the “sort_values()” method can sort the DataFrame by columns.

This write-up briefly explained different methods to Sort DataFrame by Column in Pandas. The following contents are discussed in this Python blog post:

So let’s get started!

Example 1: Sort the DataFrame By Default (Ascending) Sorting Order

The “sort_values()” sorts the dataframe by columns in pandas. By default, the “sort_value()” function sorts the data in ascending order. Let’s create a pandas dataframe and sort it out using columns via the following code:

Code:

import pandas as pd

data_value = {'ID NAME': ['Alex', 'Harry', 'Henry', 'Lily'],
    'Age': [18, 14, 17, 12],
    'Weight': [54, 44, 47, 35],
    'Height': [6.8, 6.8, 6.2, 4.7]}

dataframe_1 = pd.DataFrame(data_value)

sorted_dataframe = dataframe_1.sort_values(by='Age')
print(sorted_dataframe)

In the above output:

  • The “pandas” library is imported at the beginning of the program.
  • The dictionary variable named “data_value” is initialized by specifying its key and value.
  • The “pd.DataFrame()” function is used to create the 2D “pandas” dataframe.
  • The “df.sort_values()” function sorts the data value in ascending order by passing an argument value in its parenthesis.
  • The “by=Age” sort over DataFrame according to column age in ascending order.

Note: Ensure Pandas library is installed on the system before importing the library.

Output:

In the above output, the DataFrame is arranged in “ascending” order by following the column “Age”.

Example 2: Sort the DataFrame By Column in Descending Order

The “sort_values()” also sorts DataFrame by columns in descending order by taking the “False” value in its second parameter named “ascending”. Lets understand it via the following code:

Code:

import pandas as pd

data_value = {'ID NAME': ['Alex', 'Harry', 'Henry', 'Lily'],
    'Age': [16, 17, 21, 27],
    'Weight': [44, 45, 57, 69],
    'Height': [5.8, 6.8, 5.5, 6.2]}

dataframe_1 = pd.DataFrame(data_value)

sorted_dataframe = dataframe_1.sort_values(by='Age', ascending=False)
print(sorted_dataframe)

In the above output:

  • The “pandas” library is imported at the beginning of the program.
  • The dictionary variable named “data_value” is initialized by specifying its key and value.
  • pd.DataFrame()” function is used to create the 2D pandas dataframe.
  • The “df.sort_values()” function sorts the data value in descending order by passing the “False” value in an argument.
  • The “by=Age” sort over DataFrame according to column age in descending order.

Output:

In the above output, the DataFrame is arranged in “descending” order by following column “Age”.

Example 3: Sort DataFrame With Multiple Columns in Pandas

One can sort DataFrame by multiple columns in pandas by passing multiple column names in its first parameter named “by”. All data of DataFrame is sorted according to these two columns. Let’s see an example of code given below:

Code:

import pandas as pd
 
data_value = {'Brand': ['Cadillac','Chevrolet','Ford','GMC'],
        'Price': [92090,95090,97090,95090],
        'Model': [2015,2017,2017,2022]
        }
 
dataframe_1 = pd.DataFrame(data_value, columns=['Model','Brand','Price'])

# Model and Price (sort by multiple columns)
dataframe_1.sort_values(by=['Model','Price'], inplace=True)

print (dataframe_1)

In the above output:

  • The “pandas” library is imported at the beginning of the program.
  • The dictionary variable named “data_value” is initialized.
  • The “pd.DataFrame()” function is used to create the 2D pandas dataframe. And the position of the “Keys” of the dictionary is assigned using the “columns[]” parameter.
  • The “df.sort_values()” function sorts the data value in ascending order by passing the “True” value in its “inplace” parameter.
  • The parameter “by=‘Model’, ‘Price’”sorts the DataFrame according to two columns i.e., Model and Price in ascending order.
  • If the value of one column is the same such as “Ford” and “Chevrolet” model number is “2017” then it will arrange the dataframe according to other columns named “Price”.

Output:

In the above output, the dataframe is arranged in ascending order according to multiple columns, “Model” and “Price”.

That’s all from this guide!

Conclusion

In Python, “sort_values()” methods with “default parameter”, “descending order” and “multiple columns” are used to sort DataFrame by columns in Pandas. By default, the “sort_values()” function sorts the dataframe by column in ascending order. To sort data in descending order, the “False” value is passed into the ascending parameter. The “sort_values()” methods can also sort the DataFrame according to “Multiple Columns” in ascending and descending order. This tutorial provides complete details on how to sort DataFrame by columns in pandas.