Pandas DataFrame.reset_index()
Last Updated :
11 Jul, 2025
The reset_index() method in Pandas is used to manage and reset the index of a DataFrame. It is useful after performing operations that modify the index such as filtering, grouping or setting a custom index. By default reset_index() reverts to a clean, default integer-based index (0, 1, 2, ...) which makes the DataFrame easier to work with.
Now lets see a basic example:
Python
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)
df_reset = df.reset_index()
print("\nDataFrame after reset_index():")
print(df_reset)
Output:
Basic exampleThis 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 to reset (useful for multi-level indices). It can be an integer, string or a list of levels.
- drop (default: False): If True, removes the old index instead of adding it as a column.
- 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: The reset_index() method returns a new DataFrame with the index reset, unless inplace=True is specified. In that case, the original DataFrame is modified directly without creating a new one.
Lets see some other examples for DataFrame.reset_index() to understand it in a better way.
Example 1: Resetting Index of a Pandas DataFrame
In this example, we will set the "First Name" column as the index of the DataFrame and then reset the index using the reset_index() method. This process will move the custom index back to a regular column and restore the default integer-based index.
Here we will be using a random employee dataset which you can download from here.
Python
import pandas as pd
data = pd.read_csv("/content/employees.csv")
print("Original Employee Dataset:")
display(data.head())
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:
Original Employee Dataset
After Setting 'First Name' as Index
After Resetting IndexExample 2: Resetting the Index After Filtering Data
When filtering a DataFrame, the original row indices are retained which can lead to inconsistencies during further operations. Using reset_index() ensures that the index is clean and sequential.
Python
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)
df_it = df[df['Dept'] == 'IT']
print("\nFiltered DataFrame:")
print(df_it)
df_it = df_it.reset_index(drop=True)
print("\nFiltered DataFrame after reset_index():")
print(df_it)
Output:
Resetting Index After Filtering DataThe filtered DataFrame (df_filtered) contains only employees from the IT department but retains the original row indices (1 and 2). By applying reset_index(drop=True), we remove the old indices and replace them with a clean, default integer index (0, 1) which results in a more structured and easier-to-analyze DataFrame.
Example 3: Resetting Index for Multi-Level DataFrames
If our DataFrame has a multi-level index, reset_index() can reset one or more of the index levels, turning them back into regular columns.
Python
import pandas as pd
data = {'Region': ['North', 'North', 'South', 'South'],
'City': ['New York', 'Los Angeles', 'Miami', 'Houston'],
'Population': [8.4, 3.9, 0.4, 2.3]}
df = pd.DataFrame(data)
df.set_index(['Region', 'City'], inplace=True)
print("DataFrame with Multi-Level Index:")
print(df)
df_reset = df.reset_index(level='City')
print("\nDataFrame after resetting the 'City' level of the index:")
print(df_reset)
Output:
Resetting Index for Multi-Level DataFramesKey Use Cases of reset_index()
- Simplifying Data Manipulation: After performing operations like filtering or sorting, we may want to reset the index to have a clean, sequential index.
- Handling Multi-Level Indexes: When working with multi-level indexes, it 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.
By understanding and using reset_index(), we can efficiently manage and reorganize our DataFrame's index which makes our data easier to manipulate and analyze in various situations.
Similar Reads
Python | Delete rows/columns from DataFrame using Pandas.drop() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages which makes importing and analyzing data much easier. In this article, we will how to delete a row in Excel using Pandas as well as delete
4 min read
Pandas dataframe.groupby() Method Pandas groupby() function is a powerful tool used to split a DataFrame into groups based on one or more columns, allowing for efficient data analysis and aggregation. It follows a "split-apply-combine" strategy, where data is divided into groups, a function is applied to each group, and the results
6 min read
Pandas DataFrame corr() Method Pandas dataframe.corr() is used to find the pairwise correlation of all columns in the Pandas Dataframe in Python. Any NaN values are automatically excluded. To ignore any non-numeric values, use the parameter numeric_only = True. In this article, we will learn about DataFrame.corr() method in Pytho
4 min read
Pandas query() Method Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Analyzing data requires a lot of filtering operations. Pandas Dataframe provide many
2 min read
Pandas dataframe.insert()-Python DataFrame.insert() function in pandas inserts a new column into a DataFrame at a specified position. It allows you to specify the column index, column label and values to insert. This is particularly useful when you want to place a new column in a specific position instead of just appending it at th
4 min read
Pandas dataframe.sum() DataFrame.sum() function in Pandas allows users to compute the sum of values along a specified axis. It can be used to sum values along either the index (rows) or columns, while also providing flexibility in handling missing (NaN) values. Example:Pythonimport pandas as pd data = { 'A': [1, 2, 3], 'B
4 min read
Pandas DataFrame mean() Method Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas DataFrame mean()Â Pandas dataframe.mean() function returns the mean of the value
2 min read
Python | Pandas dataframe.median() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.median() function return the median of the values for the requested a
2 min read
Python | Pandas Series.std() Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.std() function return sample
2 min read
Apply function to every row in a Pandas DataFrame Applying a function to every row in a Pandas DataFrame means executing custom logic on each row individually. For example, if a DataFrame contains columns 'A', 'B' and 'C', and you want to compute their sum for each row, you can apply a function across all rows to generate a new column. Letâs explor
3 min read