Setting & Resetting Index
The index is the row label in a DataFrame. You can customize it for better data organization and lookup.
Default Index
By default, Pandas uses integer index starting from 0:
Loading Pandas Playground...
Setting a Column as Index
Use set_index() to make a column the index:
Loading Pandas Playground...
Resetting the Index
Use reset_index() to move the index back to a column:
Loading Pandas Playground...
Multi-Level Index
Create hierarchical indexes:
Loading Pandas Playground...
Modifying Index in Place
Use inplace=True to modify the original DataFrame:
Loading Pandas Playground...
Renaming the Index
Give your index a name:
Loading Pandas Playground...
Exercise: Set Index
Loading Exercise...
Exercise: Reset Index
Loading Exercise...
Key Points
set_index('column')makes a column the indexreset_index()moves index back to a columnreset_index(drop=True)discards the indexinplace=Truemodifies the original DataFrame- Multi-level indexes use tuples for lookup
- Index enables fast
.loc[]lookups

