Pandas DataFrame – Set Index

You can set one or more columns as the index of a Pandas DataFrame using the set_index() method.

In this tutorial, we will learn how to set the index for a Pandas DataFrame, with the help of examples.

Syntax

The syntax of set_index() method is:

df = df.set_index('column_name')

where 'column_name' is the column you want to set as the index.

The set_index() method returns a new DataFrame with the column set as index. The original DataFrame is not modified.

Set Single Column as Index

In the following Python example, we will initialize a DataFrame and set a specific column as the index of the DataFrame using the set_index() method.

Python Example

import pandas as pd

# initialize a dataframe
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
})

# set 'Name' column as index
df = df.set_index('Name')
print(df)

Output

         Age         City
Name                       
Alice     25      New York
Bob       30  Los Angeles
Charlie   35      Chicago

Here, we used the set_index() method to set the ‘Name’ column as the index of the DataFrame.

Set Multiple Columns as Index

In the following example, we will set multiple columns as the index for the DataFrame.

Python Example

import pandas as pd

# initialize a dataframe
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
})

# set 'Name' and 'Age' columns as index
df = df.set_index(['Name', 'Age'])
print(df)

Output

                  City
Name     Age            
Alice    25     New York
Bob      30 Los Angeles
Charlie  35     Chicago

In this example, both ‘Name’ and ‘Age’ columns are set as the index for the DataFrame.

Conclusion

In this Pandas Tutorial, we learned how to set a single or multiple columns as the index of a DataFrame using the set_index() method.