Pandas dataframe.sort_index()
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) andaxis=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
, andheapsort
(default isquicksort
). - 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
# 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.
# 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
# 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
# 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.