Pandas DataFrame sort_index() Function

Pandas DataFrame sort_index() Function

This tutorial explains how we can sort a Pandas DataFrame based on an index using the pandas.DataFrame.sort_index() method. We will use the DataFrame displayed in the above example to explain how we can sort a Pandas DataFrame based on index values. import pandas as pd pets_df = pd.DataFrame( { "Pet": ["Dog", "Cat", "Rabbit", "Fish"], "Name": ["Rocky", "Luna", "Coco", "Finley"], "Age(Years)": [3, 5, 5, 4], }, index=["4", "2", "1", "3"], ) print(pets_df) Output:

Tags

Pandas DataFrame Pandas Series Pandas Series Pandas Core Pandas DataFrame Plot Python DataFrame

Most Popular Articles

Recently Updated Articles