import pandas as pd
my_dict={'NAME':['Ravi','Raju','Alex','Ron','King','Jack'],
'ID':[1,2,3,4,5,6],
'MATH':[80,40,70,70,70,30],
'ENGLISH':[80,70,40,50,60,30]}
df = pd.DataFrame(data=my_dict)
print(df) # before reset_index()
df2=df.reset_index() # another dataframe df2 is used
print(df2)
When we reset the index, the old index is added as a column, and a new sequential index is used.
NAME ID MATH ENGLISH
0 Ravi 1 80 80
1 Raju 2 40 70
2 Alex 3 70 40
3 Ron 4 70 50
4 King 5 70 60
5 Jack 6 30 30
index NAME ID MATH ENGLISH
0 0 Ravi 1 80 80
1 1 Raju 2 40 70
2 2 Alex 3 70 40
3 3 Ron 4 70 50
4 4 King 5 70 60
5 5 Jack 6 30 30
df.reset_index(drop=False) # default
Output is here for drop=False
index NAME ID MATH ENGLISH
0 0 Ravi 1 80 80
1 1 Raju 2 40 70
2 2 Alex 3 70 40
3 3 Ron 4 70 50
4 4 King 5 70 60
5 5 Jack 6 30 30
Check that one index column is added in above column. Now let us see the output with dropt=True
df.reset_index(drop=True)
Output ( index column is removed )
NAME ID MATH ENGLISH
0 Ravi 1 80 80
1 Raju 2 40 70
2 Alex 3 70 40
3 Ron 4 70 50
4 King 5 70 60
5 Jack 6 30 30
df.reset_index(inplace=False)
Output
NAME ID MATH ENGLISH
0 Ravi 1 80 80
1 Raju 2 40 70
2 Alex 3 70 40
3 Ron 4 70 50
4 King 5 70 60
5 Jack 6 30 30
If we set in_place=True then the original DataFrame is modified. Let us check the DataFrame before and after using reset_index() while keeping the in_place=True. You can see the original DataFrme is changed.
print(df)
df.reset_index(inplace=True)
print(df)
Output
NAME ID MATH ENGLISH
0 Ravi 1 80 80
1 Raju 2 40 70
2 Alex 3 70 40
3 Ron 4 70 50
4 King 5 70 60
5 Jack 6 30 30
index NAME ID MATH ENGLISH
0 0 Ravi 1 80 80
1 1 Raju 2 40 70
2 2 Alex 3 70 40
3 3 Ron 4 70 50
4 4 King 5 70 60
5 5 Jack 6 30 30
df = df.reset_index()
df = df.rename(columns={"index":"New_ID"})
df['New_ID'] = df.index + 100 # starting from 100
You can also use reset_index with MultiIndex.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.