0
0
Pandasdata~5 mins

Setting a column as index in Pandas

Choose your learning style9 modes available
Introduction
Setting a column as index helps you organize your data better and makes it easier to find rows by that column.
When you want to label rows with meaningful names instead of numbers.
When you need to quickly look up data by a specific column like IDs or dates.
When you want to sort or group data based on a particular column.
When preparing data for analysis that requires a unique identifier per row.
Syntax
Pandas
df.set_index('column_name', inplace=False, drop=True)
Use 'inplace=True' to change the original DataFrame directly.
If 'drop=True', the column is removed from data and becomes the index.
Examples
Sets the 'ID' column as the index and returns a new DataFrame without changing the original.
Pandas
df.set_index('ID')
Sets the 'Date' column as the index and changes the original DataFrame.
Pandas
df.set_index('Date', inplace=True)
Sets the 'Name' column as the index but keeps it as a regular column too.
Pandas
df.set_index('Name', drop=False)
Sample Program
This creates a DataFrame with three columns and sets 'ID' as the index without changing the original DataFrame.
Pandas
import pandas as pd

data = {'ID': [101, 102, 103], 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)

# Set 'ID' as index
new_df = df.set_index('ID')
print(new_df)
OutputSuccess
Important Notes
The index helps you access rows faster by label instead of position.
If you want to reset the index back to numbers, use df.reset_index().
Summary
Setting a column as index labels rows with that column's values.
Use set_index() with 'inplace=True' to modify the original DataFrame.
You can keep or drop the column from data when setting it as index.