Pandas dataframe.sort_index()
Last Updated :
23 Dec, 2024
Pandas is one of those packages and makes importing and analyzing data much easier. When working with DataFrames, Pandas is used for handling tabular data. Let's learn Pandas DataFrame sort_index()
method, which is used to sort the DataFrame based on index or column labels.
Pandas sort_index() function sorts a DataFrame by its index labels (row labels) or column labels. This method allows us to rearrange the data based on the axis labels, without changing the data itself. It’s important to note that you can choose the sorting algorithm: quicksort, mergesort, or heapsort. The default is quicksort.
Syntax: DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind='quicksort', na_position='last', sort_remaining=True, by=None)
Parameters of sort_index()
- axis: Defines whether to sort by index (rows) or columns. Use
axis=0
for index (rows) and axis=1
for columns. - level: If specified, sorts on the values of a particular index level.
- ascending: Specifies whether to sort in ascending or descending order. Default is
True
for ascending. - inplace: If
True
, sorts the DataFrame in-place without returning a new object. - kind: The sorting algorithm. Options are
quicksort
, mergesort
, and heapsort
(default is quicksort
). - na_position: Where to place NaN values ('first' or 'last'). Default is 'last'.
- sort_remaining: If
True
, when sorting by level, it sorts by other levels after sorting the specified level. - by: A column or list of columns to sort by.
How Does sort_index()
Work?
You can get the csv file here .
Example 1: Sort DataFrame by Index Labels
Python
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# Print the dataframe
df

As we can see in the output, the index labels are already sorted i.e. (0, 1, 2, ....). So we are going to extract a random sample out of it and then sort it for the demonstration purpose.
Lets extract a random sample of 15 elements from the dataframe using dataframe.sample() function.
Python
# extract the sample dataframe from "df"
# and store it in "sample_df"
sample_df = df.sample(15)
# Print the sample data frame
sample_df

Note : Every time we execute dataframe.sample() function, it will give different output. Let's use the dataframe.sort_index() function to sort the dataframe based on the index labels
Python
# sort by index labels
sample_df.sort_index(axis = 0)
Output :

As we can see in the output, the index labels are sorted.
Example 2: Sort DataFrame by Column Labels
Python
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.read_csv("nba.csv")
# sorting based on column labels
df.sort_index(axis = 1)
Output :

sort_index() function in Pandas sorts the DataFrame by index or columns. By understanding the parameters like axis, ascending, and kind, you can customize the sorting behavior to suit your needs.
Similar Reads
Pandas DataFrame.reset_index() In Pandas, reset_index() method is used to reset the index of a DataFrame. By default, it creates a new integer-based index starting from 0, making the DataFrame easier to work with in various scenarios, especially after performing operations like filtering, grouping or multi-level indexing. Example
3 min read
Pandas Dataframe Index Index in pandas dataframe act as reference for each row in dataset. It can be numeric or based on specific column values. The default index is usually a RangeIndex starting from 0, but you can customize it for better data understanding. You can easily access the current index of a dataframe using th
3 min read
Pandas DataFrame index Property In Pandas we have names to identify columns but for identifying rows, we have indices. The index property in a pandas dataFrame allows to identify and access specific rows within dataset. Essentially, the index is a series of labels that uniquely identify each row in the DataFrame. These labels can
6 min read
Reset Index in Pandas Dataframe Letâs discuss how to reset the index in Pandas DataFrame. Often We start with a huge data frame in Pandas and after manipulating/filtering the data frame, we end up with a much smaller data frame. When we look at the smaller data frame, it might still carry the row index of the original data frame.
6 min read
Sorting rows in pandas DataFrame Pandas DataFrame is two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). We often need to do certain operations on both rows and column while handling the data. Letâs see how to sort rows in pandas DataFrame. Code #1: Sorting rows by Sc
2 min read