DataFrame.reset_index()

Pandas set_index()

Create index using columns .

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]}
my_data = pd.DataFrame(data=my_dict)
my_data.reset_index()
Output

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.
my_data.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
my_data.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.
my_data.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(my_data)
my_data.reset_index(inplace=True) 
print(my_data)
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
You can get more examples of using date column at Exercise3

Pandas date_range() to_datetime() period_range()


plus2net.com



Post your comments , suggestion , error , requirements etc here




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