Skip to main content

df.reset_index()

df.reset_index(drop=False)

Moves the index to a column.

Input:
drop : boolean, default False
If False, adds index as a column to the DataFrame. If True, doesn't add index into the DataFrame.
Returns:
df_without_idx - A new DataFrame with or without the column set as the index
Return Type:
DataFrame

pets = pets.set_index('ID')
pets
IndexSpeciesColorWeightAgeIs_CatOwner_Comment
dog_001dogblack405False There are no bad dogs, only bad owners.
cat_001catgolden1.50.2TrueMy best birthday present ever!!!
cat_002catblack159True****All you need is love and a cat.****
dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
dog_003dogblack250.5FalseBe the person your dog thinks you are.
ham_001hamsterblack13FalseNo, thank you!
ham_002hamstergolden0.250.2FalseNo, thank you!
cat_003catblack100TrueNo, thank you!
pets.reset_index()
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
2cat_002catblack159True****All you need is love and a cat.****
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
5ham_001hamsterblack13FalseNo, thank you!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!


Note that when drop=True, the previous index will be dropped instead of becoming a new column in the DataFrame.

pets.reset_index(drop=True)
IndexSpeciesColorWeightAgeIs_CatOwner_Comment
0dogblack405False There are no bad dogs, only bad owners.
1catgolden1.50.2TrueMy best birthday present ever!!!
2catblack159True****All you need is love and a cat.****
3dogwhite802FalseLove is a wet nose and a wagging tail.
4dogblack250.5FalseBe the person your dog thinks you are.
5hamsterblack13FalseNo, thank you!
6hamstergolden0.250.2FalseNo, thank you!
7catblack100TrueNo, thank you!