How to Sort in Pandas using Indexing?

To sort a Pandas DataFrame by index, you can use DataFrame.sort_index() method.

To specify whether the method has to sort the DataFrame in ascending or descending order of index, you can set the named boolean argument ascending to True or False respectively.

When the index is sorted, respective rows are rearranged.

Syntax: DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’, sort_remaining=True, by=None)

Example:

# importing pandas as pd
import pandas as pd
 
# Creating the dataframe
df = pd.read_csv("nba.csv")
 
# Print the dataframe
df