How to Sort Pandas DataFrame by Index?

The open-source module named “Pandas” provides various functions to store, organize, and manipulate data in Python. The pandas “DataFrame” is the 2-D representation of Data that organizes simple data in rows and columns well-structured and well-organized manner. 

To work with DataFrame, various functions are provided by the panda’s module in Python. For instance, to sort the DataFrame, the “df.sort_index()” function is used in Python.

This Python article will discuss the following method to sort the Pandas DataFrame by index.

How to Sort Pandas DataFrame by Index Using df.sort_index() Function?

The “df.sort_index()” function is used to sort the input Pandas DataFrame in descending and ascending order. This function also sorts the Pandas DataFrame according to the columns label. The syntax of the “df.sort_index()” function is shown below:

dataframe.sort_index(axis, level, ascending, inplace, kind, na_position, sort_remaining, ignore_index, key)

In the above syntax:

  • The “axis” parameter is used to sort the DataFrame according to index and column labels. The “axis=0” indicates the index label, and “axis=1” indicates the column label.
  • The optional “ascending” parameter is used to sort the given data frame in ascending or descending order. The default value is set as “True”, which indicates that the DataFrame will be sorted in ascending order.
  • The “inplace” parameter is set to “False” by default which indicates that the sorting will be performed on a new DataFrame. If the value is “True, ” the sorting operation will be performed on the original DataFrame.

Example 1: Sort Pandas DataFrame by Index in Ascending Order

The “df.sort_index()” function takes the Pandas “DataFrame” as an argument and sorts the index in ascending order. Here is an example:

Code:

import pandas
data_frame = pandas.DataFrame([['ALEX', 21, 6.6],['LILY', 21, 4.7],
                              ['JOSEPH', 22, 5.7],['JOHN', 27, 6.2]],
                              columns=['NAME', 'AGE', 'HEIGHT'])
print('DataFrame Created: \n',data_frame)
output = data_frame.sort_index(ascending=True)
print('\nAfter Sorting Index in Ascending Order: \n\n', output)

In the above code:

  • The “pandas.DataFrame()” function creates the DataFrame by accepting the multiple lists along with the list of column names as arguments.
  • The “data_frame.sort_index()” function accepts the parameter “ascending=True” as an argument and returns the DataFrame in ascending order. 

Output:

The above output shows that the “DataFrame” has been created and sorted in ascending order using the “sort_index()” method.

Example 2: Sort Pandas DataFrame by Index in Descending Order

To sort pandas DataFrame in descending order, you need to specify the value of the “ascending” parameter as “false” to the “data_frame.sort_index()” function.

Code:

import pandas
data_frame = pandas.DataFrame([['ALEX', 21, 6.6],['LILY', 21, 4.7],
                              ['JOSEPH', 22, 5.7],['JOHN', 27, 6.2]],
                              columns=['NAME', 'AGE', 'HEIGHT'])
print('DataFrame Created: \n',data_frame)
output = data_frame.sort_index(ascending=False)
print('\nAfter Sorting Index in Descending Order: \n\n', output)

In the above code:

  • The “pd.DataFrame()” function is used to create the DataFrame containing the three columns and four rows with default index.
  • The parameter “ascending=False” is used to sort the given data frame by index in reversed/descending order.

Output:

The above output shows that the given data frame has been sorted in descending order.

Example 3: Sort Pandas DataFrame According to Columns Labels

To sort the Pandas DataFrame according to column labels, we can use the “axis” parameter value. The “axis” parameter in the below code is used to sort the DataFrame according to column labels:

Code:

import pandas
data_frame = pandas.DataFrame([['ALEX', 21, 6.6],['LILY', 21, 4.7],
                              ['JOSEPH', 22, 5.7],['JOHN', 27, 6.2]],
                              columns=['NAME', 'AGE', 'HEIGHT'])
print('DataFrame Created: \n',data_frame)
output = data_frame.sort_index(axis = 1)
print('\nAfter Sorting Index According to Columns Label: \n\n', output)

In the above code:

  • The “axis=1” is passed as an argument to the “df.sort_index()” function to sort the given DataFrame based on the columns label.

Output:

The above output verified that the given DataFrame had been sorted according to column labels.

Conclusion

To sort the pandas DataFrame by index in ascending or descending order, the “df.sort_index()” function is used in Python. Users can also sort a DataFrame according to column labels using the “sort_index()” function. The complete process of sorting the DataFrame by index using various examples has been discussed in this Python guide.