0
0
Data Analysis Pythondata~10 mins

Renaming columns in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Renaming columns
Start with DataFrame
Choose columns to rename
Create mapping: old_name -> new_name
Apply rename method
Get DataFrame with new column names
End
We start with a DataFrame, decide which columns to rename, create a mapping from old to new names, apply the rename, and get the updated DataFrame.
Execution Sample
Data Analysis Python
import pandas as pd

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

df_renamed = df.rename(columns={'A': 'Alpha', 'B': 'Beta'})
This code renames columns 'A' to 'Alpha' and 'B' to 'Beta' in a simple DataFrame.
Execution Table
StepActionDataFrame Columns BeforeRename MappingDataFrame Columns After
1Create DataFrame['A', 'B']{}['A', 'B']
2Define rename mapping['A', 'B']{'A': 'Alpha', 'B': 'Beta'}['A', 'B']
3Apply rename method['A', 'B']{'A': 'Alpha', 'B': 'Beta'}['Alpha', 'Beta']
4Store renamed DataFrame['A', 'B']{'A': 'Alpha', 'B': 'Beta'}['Alpha', 'Beta']
💡 Rename applied, columns changed from ['A', 'B'] to ['Alpha', 'Beta']
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
df.columnsN/A['A', 'B']['A', 'B']['A', 'B']['A', 'B']
rename_mappingN/AN/A{'A': 'Alpha', 'B': 'Beta'}{'A': 'Alpha', 'B': 'Beta'}{'A': 'Alpha', 'B': 'Beta'}
df_renamed.columnsN/AN/AN/A['Alpha', 'Beta']['Alpha', 'Beta']
Key Moments - 2 Insights
Why does the original DataFrame 'df' keep its old column names after rename?
Because the rename method returns a new DataFrame by default and does not change 'df' in place. See execution_table step 3 and 4 where 'df_renamed' has new columns but 'df' remains unchanged.
What happens if you rename a column not present in the DataFrame?
The rename mapping for that column is ignored and the DataFrame columns stay the same. Only existing columns are renamed. This is why the mapping keys must match existing column names.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what are the columns of the DataFrame returned by rename?
A['Alpha', 'Beta']
B['A', 'B']
C['Alpha', 'B']
D['A', 'Beta']
💡 Hint
Check the 'DataFrame Columns After' column at step 3 in execution_table.
According to variable_tracker, what is the value of df.columns after step 4?
A['Alpha', 'Beta']
B['A', 'B']
C['Alpha', 'B']
D['A', 'Beta']
💡 Hint
Look at the row for df.columns in variable_tracker after step 4.
If you want to rename columns in place without creating a new DataFrame, what should you do?
AUse df.rename(columns=..., inplace=True)
BAssign df = df.rename(columns=...)
CAll of the above
DUse df.columns = new_names list
💡 Hint
All these methods can change column names; see common pandas usage.
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 rename in place.
Only columns in the mapping are renamed.
Unmapped columns stay the same.
Full Transcript
We start with a DataFrame with columns 'A' and 'B'. We create a mapping to rename 'A' to 'Alpha' and 'B' to 'Beta'. Applying df.rename with this mapping returns a new DataFrame with columns 'Alpha' and 'Beta'. The original DataFrame keeps its columns unless we specify inplace=True. This process helps us change column names clearly and safely.