Pandas DataFrame set_index() Method

Pandas set_index() method is used to set the DataFrame index (row labels) using one or more existing columns.  It can replace the existing index or expand it.

Syntax

DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)

Parameters

Name Description
keys Column label or list of column labels to be set as index.
drop If True, the column(s) used as a new index will be removed from the DataFrame; if False, they will remain in the DataFrame.
append If True, append columns to the existing index; if False, replace the existing index.
inplace If True, modify the DataFrame in place; if False, return a new DataFrame.
verify_integrity If True, check for duplicates in the new index; if False, no check is performed.

Return value

It returns a new DataFrame with the set index if inplace=False or None if inplace=True.

Example 1: Setting a single column as an index

Visual Representation of Pandas DataFrame set_index() Method

import pandas as pd

df = pd.DataFrame({'A': ['a', 'b', 'c'],
                   'B': [1, 2, 3],
                   'C': [4, 5, 6]})

df.set_index('A', inplace=True)

print(df)

Output

Output of Pandas DataFrame set_index() method

Example 2: Setting multiple columns as an index

Setting multiple columns as an index in Pandas DataFrame

import pandas as pd

df = pd.DataFrame({'A': ['a', 'b', 'c'],
                   'B': [1, 2, 3],
                   'C': [4, 5, 6]})

df.set_index(['A', 'B'], inplace=True)

print(df)

Output

Output of Setting multiple columns as an index

Example 3: Keeping the column in DataFrame after setting it as an index

Keeping the column in DataFrame after setting it as an index

import pandas as pd

df = pd.DataFrame({'A': ['a', 'b', 'c'],
                   'B': [1, 2, 3],
                   'C': [4, 5, 6]})

df.set_index('A', drop=False, inplace=True)

print(df)

Output

Output of Keeping the column in DataFrame

Example 4: Appending to an existing index

Appending to an existing index

import pandas as pd

df = pd.DataFrame({'A': ['a', 'b', 'c'],
                   'B': [1, 2, 3],
                   'C': [4, 5, 6]})

df.set_index('A', inplace=True)
df.set_index('B', append=True, inplace=True)

print(df)

Output

Output of appending to an existing index

The set_index() with multiple columns creates a multi-level index (hierarchical index), which is extremely powerful for grouping and pivoting data.

The reset_index() method is complementary to set_index() and can revert the index to a column or create a default integer index.

1 thought on “Pandas DataFrame set_index() Method”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.