Renaming & Relabelling Columns and Index
Renaming is how you change the labels on a DataFrame — the column headers and index names — without touching the values stored underneath them.
Learn Renaming & Relabelling Columns and Index in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise…
Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
Learn to rename selected columns with a dict, relabel the index, replace the whole header list, clean names with .str methods, and tag columns with add_prefix and add_suffix.
rename() takes a dictionary that maps old label to new label . Only the labels you list change; everything else is left exactly as it was. That makes it the safe choice when a couple of column names came in wrong and the rest are fine. The same method relabels the row index via index={...} .
When the whole header row is a mess, it is often quicker to overwrite it entirely. Assigning a list to df.columns replaces all the names at once. The catch: your list must have exactly one name per column, in the right order, or pandas raises a length-mismatch error.
Sometimes you want to mark every column with a tag — a quarter, a source name, a units note. add_prefix() bolts a string onto the front of every column name, add_suffix() onto the back. Both return a new DataFrame and leave the values alone. They shine right before a merge, when you need to tell columns from two tables apart.
The replacement list has the wrong number of names:
✅ Fix: give exactly one name per column, or use rename:
Forgot to assign the result back (or a typo in the old name):
An exported sheet has ugly headers. Clean them three ways.
Lesson complete — your labels are clean!
You can rename selected columns and index labels with rename , replace a whole header via df.columns , scrub names with .str methods, and tag them with add_prefix / add_suffix .
🚀 Up next: Counting & Uniques — summarise a column with value_counts, unique, and nunique.
Practice quiz
How do you rename column A to x with rename()?
- df.rename(columns={'A': 'x'})
- x
Answer: df.rename(columns={'A': 'x'}). Pass a dict to the columns= argument: {'A': 'x'}.
What does rename() return by default?
- None, it edits in place
- A new DataFrame, leaving the original unchanged
- Just the new column names
- A boolean
Answer: A new DataFrame, leaving the original unchanged. rename() returns a new frame by default; use inplace=True to mutate.
How do you replace ALL column labels at once via the attribute?
- x
- y
Assigning a list to df.columns replaces every column name.
Which call renames index labels rather than columns?
- df.rename(columns={...})
- df.rename(labels={...})
- df.rename(rows={...})
- df.rename(index={'r0': 'first'})
Answer: df.rename(index={'r0': 'first'}). Use the index= argument to rename row labels.
What does df.add_prefix('p_') do?
- Adds 'p_' to the front of every column name
- Adds a new column 'p_'
- Prefixes every value with p_
- Renames the index
Answer: Adds 'p_' to the front of every column name. add_prefix prepends the string to each column label.
What does df.add_suffix('_x') produce for column A?
- x_A
- A_x
- A
- _xA
Answer: A_x. add_suffix appends the string, turning A into A_x.
How do you lowercase every column name?
- df.lower()
- df.columns.lower()
- df.rename(columns=str.lower)
- df.tolower()
Answer: df.rename(columns=str.lower). Pass the function str.lower to columns= so it maps each name.
What does the inplace=True argument do in rename()?
- Returns a copy
- Renames only the first column
- Reverses the names
- Modifies the original DataFrame and returns None
Answer: Modifies the original DataFrame and returns None. inplace=True mutates the frame directly and returns None.
Which method sets new labels on a chosen axis?
- x
- y
Answer: x. set_axis assigns new labels along axis=0 (rows) or axis=1 (columns).
If a key in the rename dict matches no column, what happens?
- It raises a KeyError
- That entry is ignored and other renames still apply
- All columns are dropped
- The frame becomes empty
Answer: That entry is ignored and other renames still apply. By default rename ignores keys that don't match; only matching labels change.