Pandas DataFrame.reset_index()
In Pandas, reset_index() method is used to reset the index of a DataFrame. By default, it creates a new integer-based index starting from 0, making the DataFrame easier to work with in various scenarios, especially after performing operations like filtering, grouping or multi-level indexing. Example:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Max'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data, index=['a', 'b', 'c'])
print("Original DataFrame:")
print(df)
# Resetting the index
df_reset = df.reset_index()
print("\nDataFrame after reset_index():")
print(df_reset)
Output
Original DataFrame: Name Age a Alice 25 b Bob 30 c Max 35 DataFrame after reset_index(): index Name Age 0 a Alice 25 1 b Bob 30 2 c Max 35
Explanation: This code creates a DataFrame with 'Name' and 'Age' columns and a custom index ('a', 'b', 'c'). The reset_index() function moves these index labels into a new 'index' column and replaces them with default row numbers (0, 1, 2).
Syntax
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
Parameters:
- level (optional): Specifies the index level(s) to remove or reset. It can be an integer, string, or a list of levels.
- drop (default: False): If set to True, the index column is removed entirely instead of being added as a column in the DataFrame.
- inplace (default: False): If True, modifies the original DataFrame in place. If False, returns a new DataFrame.
- col_level (default: 0): Used to select the level of the column to insert the index labels.
- col_fill (default: ''): If the DataFrame has multiple column levels, this determines how missing levels are filled in the column headers.
Returns: It returns a new DataFrame with the index reset unless inplace=True is used, in which case the original DataFrame is modified.
Examples of Pandas DataFrame.reset_index()
Example 1: Resetting Index of a Pandas DataFrame
In this example, we will set the "First Name" column as the index and then reset the index using reset_index(). Dataset Link: Employee.csv
import pandas as pd
data = pd.read_csv("employees.csv")
print("Original Employee Dataset:")
display(data.head())
# Set and reset "First Name" as index
data.set_index("First Name", inplace=True)
print("\nAfter Setting 'First Name' as Index:")
display(data.head())
data.reset_index(inplace=True)
print("\nAfter Resetting Index:")
display(data.head())
Output:


Explanation: We set the "First Name" column as the index using set_index(), replacing the default integer index. After displaying the modified dataset, we use reset_index() to restore the integer index and move "First Name" back as a regular column, then display the final dataset again.
Example 2: Resetting Index After Filtering Data
When filtering a DataFrame, the original row index is retained, which may cause inconsistencies when performing further operations. Using reset_index() helps maintain a clean index.
import pandas as pd
data = {'ID': [101, 102, 103, 104, 105],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Emma'],
'Dept': ['HR', 'IT', 'IT', 'Finance', 'HR'],
'Salary': [50000, 60000, 65000, 70000, 55000]}
df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)
# Filtering IT employees
df_it = df[df['Dept'] == 'IT']
print("\nFiltered DataFrame:")
print(df_it)
# Resetting index
df_it = df_it.reset_index(drop=True)
print("\nFiltered DataFrame after reset_index():")
print(df_it)
Output:

Explanation: The filtered DataFrame (df_filtered) is created by selecting only employees from the IT department while retaining the original row indices (1 and 2). Applying reset_index(drop=True) removes the old indices and replaces them with a default integer index (0,1), making the DataFrame more structured and easier to analyze.
Key Use Cases of reset_index()
- Simplifying Data Manipulation: After performing operations like filtering or sorting, you might want to reset the index to have a clean, sequential index.
- Handling Multi-Level Indexes: When working with multi-level indexes, reset_index() can be used to remove specific index levels without affecting the rest of the structure.
- Restoring Default Index: If you've set a custom index using the set_index() method, reset_index() restores the default integer-based index.