0
0
Pandasdata~10 mins

Renaming columns in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Renaming columns
Start with DataFrame
Choose rename method
Map old names to new names
Apply rename
Get DataFrame with new column names
End
We start with a DataFrame, decide how to rename columns, map old names to new, apply the rename, and get the updated DataFrame.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2], 'B': [3, 4]
})

# Rename columns
new_df = df.rename(columns={'A': 'Alpha', 'B': 'Beta'})
This code creates a DataFrame with columns 'A' and 'B', then renames them to 'Alpha' and 'Beta'.
Execution Table
StepActionDataFrame Columns BeforeRename MappingDataFrame Columns After
1Create DataFrame['A', 'B']{}['A', 'B']
2Prepare rename mapping['A', 'B']{'A': 'Alpha', 'B': 'Beta'}['A', 'B']
3Apply rename['A', 'B']{'A': 'Alpha', 'B': 'Beta'}['Alpha', 'Beta']
4Result stored in new_df['A', 'B']{'A': 'Alpha', 'B': 'Beta'}['Alpha', 'Beta']
💡 Rename applied, columns changed from ['A', 'B'] to ['Alpha', 'Beta']
Variable Tracker
VariableStartAfter Step 1After Step 3Final
df.columnsN/AIndex(['A', 'B'], dtype='object')Index(['A', 'B'], dtype='object')Index(['A', 'B'], dtype='object')
new_df.columnsN/AN/AIndex(['Alpha', 'Beta'], dtype='object')Index(['Alpha', 'Beta'], dtype='object')
Key Moments - 2 Insights
Why does df.columns not change after rename, but new_df.columns does?
Because rename returns a new DataFrame by default and does not modify df in place. See execution_table step 3 and 4 where new_df has renamed columns but df remains the same.
What happens if you rename a column not present in the DataFrame?
The rename mapping for missing columns is ignored, so only existing columns are renamed. This is why the mapping keys must match existing column names to have effect.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what are the columns of new_df after step 3?
A['A', 'B']
B['Alpha', 'B']
C['Alpha', 'Beta']
D['A', 'Beta']
💡 Hint
Check the 'DataFrame Columns After' column in execution_table row with Step 3
At which step does the rename mapping get prepared?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when rename mapping is set
If you want to rename columns in the original df without creating a new DataFrame, what should you do?
AAssign new_df back to df
BAll of the above
CUse df.columns = ['Alpha', 'Beta']
DUse df.rename(columns=..., inplace=True)
💡 Hint
All these methods change the original DataFrame columns; see variable_tracker for df.columns changes
Concept Snapshot
Renaming columns in pandas:
Use df.rename(columns={'old': 'new'}) to rename columns.
By default, rename returns a new DataFrame.
Use inplace=True to modify original df.
Only columns in mapping keys are renamed.
Unmapped columns stay the same.
Full Transcript
We start with a DataFrame with columns 'A' and 'B'. We prepare a rename mapping to change 'A' to 'Alpha' and 'B' to 'Beta'. Applying df.rename with this mapping returns a new DataFrame with renamed columns. The original DataFrame remains unchanged unless inplace=True is used. This process helps change column names clearly and safely.