DataFrame.reset_index()

Pandas set_index()

Reset the index of the DataFrame.

options

level : Remove all levels by default. Only remove of given level ( list , str, int )
drop: Bool, default False.
inplace : Bool, default False. Modify the original DataFrame or not
col_level: int or str, default 0. Which levels are inserted into.
col_fill : for multiple level how the levels are to be named.

options

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.
Check the difference in two print() outputs. 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

drop

If drop=False ( default ) then index will be inserted into as a column.
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

inplace

Modify the DataFrame or not. Default is False ( inplace=False ). In this case before and after using reset_index() the DataFrame remain same.
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

Add a column with sequential numbers

Once we reset the index, the old index is added as a column, (first line below ). In second line we rename() the column to 'New_ID'. In third line we replaced the New_ID column value by adding 100 to (new ) index value.
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.
You can get more examples of using date column at Exercise3

Pandas date_range() to_datetime() period_range()
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer