Pandas dataframes are quite powerful for manipulating data. Often you may require to re-index your dataframe to the default index. In this tutorial, we’ll cover how to reset the index of a pandas dataframe with some examples.
Why reset the index of a dataframe?
As you apply common operations such as filtering, removing NaNs, etc, it may happen that you end up with possibly a smaller dataframe whose indices are not continuous. For example:
import pandas as pd
# create a sample dataframe
data = {
'A': ['a1', 'a2', 'a3', 'a4'],
'B': [1, 2, 3, 4],
'C': ['c1', 'c2', 'c3', 'c4']
}
df = pd.DataFrame(data)
# filter the dataframe for only odd values of B
df_filtered_odd = df[df['B']%2!=0]
The dataframes df
and df_filtered_odd
look as follows:

Having a dataframe with non-continuous indices may not be an issue by itself but performing operations on such dataframes along with dataframes with continuous or a different index scheme could give some unintended results. For example:
df_filtered_even = df[df['B']%2==0]
print(df_filtered_odd['B']+df_filtered_even['B'])
Output:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
0 NaN
1 NaN
2 NaN
3 NaN
Name: B, dtype: float64
In the above example, we create the dataframe df_filtered_even
by filtering df
for even values of B
. Both the dataframes df_filtered_odd
and df_filtered_even
are of length two but if we perform an addition operation with both of them on the column B
we get a series of four NaNs. This happened because row operations are performed at the index level and since neither of the two dataframe had a matching index we got four NaNs.
How to reset the index of a dataframe?
You can use the pandas dataframe reset_index()
function to set the index of a dataframe to its default (i.e. continuous numbers from zero). The following is its syntax:
df.reset_index()
The above function returns a copy of your dataframe with its old index as a new column and having a continuous integer index from 0.
- Pass
drop=True
to the above function if you don’t want the old index as a new column in your dataframe. - Pass
inplace=True
if you want to modify the dataframe in-place.
Examples
Example 1: reset_index() with default parameters
import pandas as pd
data = {
'Name': ['Sam', 'Tim', 'Rahul', 'Emma', 'Kyle'],
'Age': [14, 21, 16, 18, 23],
'Country': ['UK', 'India', 'USA', 'Germany', 'France'],
'Language': ['English', 'Hindi', 'English', 'German', 'French']
}
df = pd.DataFrame(data, index=[1,3,5,7,9])
print("Before reset index:\n")
print(df)
# reset the index with default parameters
df = df.reset_index()
print("\nAfter reset index:\n")
print(df)
Output:
Before reset index:
Name Age Country Language
1 Sam 14 UK English
3 Tim 21 India Hindi
5 Rahul 16 USA English
7 Emma 18 Germany German
9 Kyle 23 France French
After reset index:
index Name Age Country Language
0 1 Sam 14 UK English
1 3 Tim 21 India Hindi
2 5 Rahul 16 USA English
3 7 Emma 18 Germany German
4 9 Kyle 23 France French
In the above example, we reset the index of the dataframe df
using reset_index() with default parameters. We can see that a new column by the name index
has been created storing the values of the old index and the dataframe’s index is reset to continuous integers from 0.
Example 2: With drop=True
parameter
import pandas as pd
data = {
'Name': ['Sam', 'Tim', 'Rahul', 'Emma', 'Kyle'],
'Age': [14, 21, 16, 18, 23],
'Country': ['UK', 'India', 'USA', 'Germany', 'France'],
'Language': ['English', 'Hindi', 'English', 'German', 'French']
}
df = pd.DataFrame(data, index=[1,3,5,7,9])
print("Before reset index:\n")
print(df)
# reset the index with drop=True
df = df.reset_index(drop=True)
print("\nAfter reset index:\n")
print(df)
Output:
Before reset index:
Name Age Country Language
1 Sam 14 UK English
3 Tim 21 India Hindi
5 Rahul 16 USA English
7 Emma 18 Germany German
9 Kyle 23 France French
After reset index:
Name Age Country Language
0 Sam 14 UK English
1 Tim 21 India Hindi
2 Rahul 16 USA English
3 Emma 18 Germany German
4 Kyle 23 France French
You can see that using the drop=True
gave a dataframe without the old index as an additional column.
The reset_index() function works with MultiIndex as well. For more, refer to its documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.