Skip to main content

df.set_index()

df.set_index(column_name)

Moves a column to the index.

Input:
column_name : string
Column name to make as the index.
Returns:
df_with_idx - A new DataFrame with 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!