How to Reset Index of Pandas DataFrame? Examples
Pandas Reset Index of DataFrame
When you concatenate, sort, join or do some rearrangements with your DataFrame, the index gets shuffled or out of order.
To reset the index of a dataframe, you can use pandas.DataFrame.reset_index() method.
In this tutorial, you'll learn how to reset the index of a given DataFrame using DataFrame.reset_index() method.
Syntax of reset_index()
The syntax of DataFrame.reset_index() function is given below.
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
To reset the index, pass the parameters drop=True and inplace=True.
Examples
1. Reset index of DataFrame using reset_index()
In this example, we will concatenate two dataframes and then use reset_index() to re-order the index of the dataframe.
Python Program
import pandas as pd
df_1 = pd.DataFrame(
[['Somu', 68, 84, 78, 96],
['Kiku', 74, 56, 88, 85],
['Ajit', 77, 73, 82, 87]],
columns=['name', 'physics', 'chemistry','algebra','calculus'])
df_2 = pd.DataFrame(
[['Amol', 72, 67, 91, 83],
['Lini', 78, 69, 87, 92]],
columns=['name', 'physics', 'chemistry','algebra','calculus'])
#concatenate dataframes
df = pd.concat([df_1, df_2])
#now the index is not in order
# reset index
df.reset_index(drop=True, inplace=True)
#print dataframe
print(df)
Output
2. Reset index of DataFrame using concat()
You can reset the index using concat() function as well. Pass in the argument ignore_index=True
to the concat() function.
Python Program
import pandas as pd
df_1 = pd.DataFrame(
[['Somu', 68, 84, 78, 96],
['Kiku', 74, 56, 88, 85],
['Ajit', 77, 73, 82, 87]],
columns=['name', 'physics', 'chemistry','algebra','calculus'])
df_2 = pd.DataFrame(
[['Amol', 72, 67, 91, 83],
['Lini', 78, 69, 87, 92]],
columns=['name', 'physics', 'chemistry','algebra','calculus'])
#reset index while concatenating
df = pd.concat([df_1, df_2], ignore_index=True)
#print dataframe
print(df)
Output
name physics chemistry algebra calculus
0 Somu 68 84 78 96
1 Kiku 74 56 88 85
2 Ajit 77 73 82 87
3 Amol 72 67 91 83
4 Lini 78 69 87 92
If you have only one dataframe whose index has to be reset, then just pass that dataframe in the list to the concat() function.
Python Program
import pandas as pd
df_1 = pd.DataFrame(
[['Somu', 68, 84, 78, 96],
['Kiku', 74, 56, 88, 85],
['Ajit', 77, 73, 82, 87]],
columns=['name', 'physics', 'chemistry','algebra','calculus'])
df_2 = pd.DataFrame(
[['Amol', 72, 67, 91, 83],
['Lini', 78, 69, 87, 92]],
columns=['name', 'physics', 'chemistry','algebra','calculus'])
#concatenate
df = pd.concat([df_1, df_2])
#now the index is not in order
#reset the index
df = pd.concat([df], ignore_index=True)
#print dataframe
print(df)
Video Tutorial - Reset Index of Pandas DataFrame
Summary
In this Pandas Tutorial, we learned how to resent index of Pandas DataFrame.