set_index() and reset_index() methods in Pandas
In Pandas, the set_index() and reset_index() methods are used to manipulate the index of a DataFrame.
• set_index() Method The set_index method assigns one or more columns to be the index of the DataFrame.
Syntax DataFrame.set_index(keys, drop=True, inplace=False)
The function has the following parameters:
- keys: Column label or list of labels to set as index.
- drop: If True (default), removes the column(s) from the data. If False, keeps the columns in the data.
- inplace: If True, modifies the original DataFrame.
Example:
Python
import pandas as pd df = pd.DataFrame({ 'ID': [101, 102, 103], 'Name': ['Ashish', 'Bobby', 'Senorita'], 'Score': [90, 85, 88] }) df_indexed = df.set_index('ID') print(df_indexed)
The output of the above code is shown below:

• reset_index() Method The reset_index() method moves the index back into a column and reset the default integer index.
Syntax DataFrame.reset_index(drop=False, inplace=False)
The function has the following parameters:
- drop: If True, does not add the index column back into the DataFrame.
- inplace: If True, modifies the original DataFrame.
Example:
Python
df_reset = df_indexed.reset_index() print(df_reset)
The output of the above code is shown below:

Summary Table
Method | Purpose | Default Behavior |
---|---|---|
set_index() | Set column(s) as the index | Removes the column from data |
reset_index() | Moves index back as a column | Adds index as a new column |