0
0
Pandasdata~10 mins

Setting a column as index in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Setting a column as index
Start with DataFrame
Choose column to set as index
Use set_index() method
New DataFrame with chosen column as index
Use DataFrame with new index for operations
You start with a table, pick a column, then use set_index() to make that column the row labels (index).
Execution Sample
Pandas
import pandas as pd

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

new_df = df.set_index('Name')
This code creates a table with names and ages, then sets the 'Name' column as the index.
Execution Table
StepActionDataFrame StateIndex ColumnOutput
1Create DataFrameName | Age Alice | 25 Bob | 30 Charlie | 35Default (0,1,2)DataFrame with default index
2Call set_index('Name')Name | Age Alice | 25 Bob | 30 Charlie | 35NameNew DataFrame with 'Name' as index
3Resulting DataFrameAge Name Alice | 25 Bob | 30 Charlie | 35NameIndex set to 'Name' column
💡 Index is set to 'Name' column, replacing default numeric index.
Variable Tracker
VariableStartAfter set_indexFinal
dfDataFrame with default indexUnchangedUnchanged
new_dfNot definedDataFrame with 'Name' as indexSame as after set_index
Key Moments - 2 Insights
Why does the original DataFrame 'df' not change after set_index?
Because set_index() returns a new DataFrame and does not modify 'df' unless you use inplace=True. See execution_table step 2 where 'df' remains unchanged.
What happens to the 'Name' column after setting it as index?
The 'Name' column becomes the index and is no longer a regular column in the new DataFrame, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the index of 'new_df' after step 2?
A'Name' column values
BDefault numeric index 0,1,2
C'Age' column values
DEmpty index
💡 Hint
Check the 'Index Column' column in execution_table row 2.
At which step does the 'Name' column become the index?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
Look at the 'Action' and 'Index Column' columns in execution_table.
If you want to change 'df' itself to have 'Name' as index, what should you do?
ACall df.set_index('Name') without assignment
BUse df.set_index('Name', inplace=True)
CAssign df = df.set_index('Name')
DRename the 'Name' column
💡 Hint
See key_moments about set_index() returning a new DataFrame.
Concept Snapshot
Use df.set_index('column_name') to make a column the index.
This returns a new DataFrame; original stays same unless inplace=True.
The chosen column becomes row labels, not a regular column.
Useful for faster lookups and clearer data organization.
Full Transcript
We start with a DataFrame having default numeric index. We pick a column, here 'Name', to become the index. Using set_index('Name') creates a new DataFrame where 'Name' is the index. The original DataFrame remains unchanged unless we assign the result back or use inplace=True. The 'Name' column moves from being a regular column to the row labels, making data easier to access by name.