Pandas set column names questions usually start with a messy CSV header: stray spaces, dollar signs, or labels nobody wants to type twice. We hit all three on the HR export below.
Quick answer: assign a list to df.columns to replace every name, or call rename() with a dictionary to change only the ones you pick:
df.columns = ["emp_id", "first_name", "salary"] # all names
df = df.rename(columns={"Emp ID": "emp_id"}) # some names
In this tutorial, I’ll show how we pandas set column names at load time, on an existing DataFrame, in bulk with string methods and after a groupby, plus the mistakes that quietly leave old names behind.
What You Need Before You Start
We tested every example with Python 3.12.5 and pandas 3.0.6. The running sample is employees.csv, a five-row payroll export whose headers came out of the HR system with extra spaces and symbols.
Emp ID, First Name,Last Name ,Department,Annual Salary ($),Hire Date,State
1001,Megan,Turner,Sales,84500.00,2026-01-12,TX
1002,Jason,Brooks,Engineering,92300.00,2026-02-03,WA
1003,Emily,Carter,Engineering,78250.00,2026-03-16,NY
1004,Marcus,Reed,Sales,101750.00,2026-04-06,CA
1005,Laura,Bennett,Finance,69900.00,2026-05-11,IL
Load it as is and you get these headers:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
df = pd.read_csv("employees.csv")
print(df.columns.tolist())
Output:
['Emp ID', ' First Name', 'Last Name ', 'Department', 'Annual Salary ($)', 'Hire Date', 'State']
Look closely. ' First Name' starts with a space and 'Last Name ' ends with one, so df["First Name"] would raise a KeyError. My guide on getting pandas column names covers more ways to inspect headers like these.
Step 1: Set Column Names When You Read or Create the DataFrame
The cleanest fix happens at load time. Pass names= to read_csv(), and add header=0 so pandas drops the file’s own header row:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
cols = ["emp_id", "first_name", "last_name", "dept",
"salary", "hire_date", "state"]
df = pd.read_csv("employees.csv", names=cols, header=0)
print(df.head(3))
Output:
emp_id first_name last_name dept salary hire_date state
0 1001 Megan Turner Sales 84500.0 2026-01-12 TX
1 1002 Jason Brooks Engineering 92300.0 2026-02-03 WA
2 1003 Emily Carter Engineering 78250.0 2026-03-16 NY

names= with header=0 swaps the file’s header for our seven clean names, and the first data row is still Megan Turner.If the file has no header line at all, use header=None with names=. More loading options, such as usecols and dtype, are in my pandas read_csv tutorial.
When you build a DataFrame from lists, the columns argument sets the names in the same call:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
rows = [[1006, "Kevin", 88400.00], [1007, "Rachel", 95100.00]]
df = pd.DataFrame(rows, columns=["emp_id", "first_name", "salary"])
print(df)
Output:
emp_id first_name salary
0 1006 Kevin 88400.0
1 1007 Rachel 95100.0
Without columns=, pandas would label them 0, 1 and 2. Dictionaries set names from their keys, as shown in creating a pandas DataFrame from a dictionary.
Step 2: Replace All Column Names on an Existing DataFrame
Already loaded the data? Assign a new list to df.columns. The list needs one name per column, in the same order:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
cols = ["emp_id", "first_name", "last_name", "dept",
"salary", "hire_date", "state"]
df = pd.read_csv("employees.csv")
df.columns = cols
print(df.head(2))
df2 = pd.read_csv("employees.csv").set_axis(cols, axis=1)
print(df2.columns.equals(df.columns))
Output:
emp_id first_name last_name dept salary hire_date state
0 1001 Megan Turner Sales 84500.0 2026-01-12 TX
1 1002 Jason Brooks Engineering 92300.0 2026-02-03 WA
True
set_axis(cols, axis=1) does the same job but returns a new DataFrame, so it fits inside a method chain. The True means both gave identical names.
Sometimes the real header sits in the first data row, for example after reading with header=None. Promote that row, then drop it. To remove a header instead, see dropping the header row of a pandas DataFrame.
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
raw = pd.read_csv("employees.csv", header=None)
print(raw.columns.tolist())
raw.columns = raw.iloc[0].str.strip()
raw.columns.name = None
raw = raw.iloc[1:].reset_index(drop=True)
print(raw.head(2))
Output:
[0, 1, 2, 3, 4, 5, 6]
Emp ID First Name Last Name Department Annual Salary ($) Hire Date State
0 1001 Megan Turner Sales 84500.00 2026-01-12 TX
1 1002 Jason Brooks Engineering 92300.00 2026-02-03 WA
Note: every column is text after this trick, because the header row was read as data. Convert numbers with astype() or reload with names=.
Step 3: Rename Specific Columns with rename()
To change only some names, pass rename() a dictionary of old name to new name. Columns you don’t mention keep their names, stray spaces included:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
df = pd.read_csv("employees.csv")
df = df.rename(columns={"Emp ID": "emp_id",
"Annual Salary ($)": "salary"})
print(df.columns.tolist())
print(df[["emp_id", "salary"]].head(3))
Output:
['emp_id', ' First Name', 'Last Name ', 'Department', 'salary', 'Hire Date', 'State']
emp_id salary
0 1001 84500.0
1 1002 92300.0
2 1003 78250.0

rename() returns a new DataFrame, so you assign it back. I prefer that over inplace=True; it reads clearly and fits pandas 3.0’s copy-on-write rules.
Step 4: Clean Every Column Name at Once
For dozens of columns, a dictionary gets long. df.columns has string methods too, so you can strip, lowercase and replace symbols in one chain:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
df = pd.read_csv("employees.csv")
df.columns = (df.columns.str.strip()
.str.lower()
.str.replace(r"[^a-z0-9]+", "_", regex=True)
.str.strip("_"))
print(df.columns.tolist())
print(df.add_prefix("hr_").columns.tolist()[:3])
print(df.rename(columns=lambda c: c.replace("_", " ").title())
.columns.tolist())
Output:
['emp_id', 'first_name', 'last_name', 'department', 'annual_salary', 'hire_date', 'state']
['hr_emp_id', 'hr_first_name', 'hr_last_name']
['Emp Id', 'First Name', 'Last Name', 'Department', 'Annual Salary', 'Hire Date', 'State']

Annual Salary ($) into annual_salary, and add_prefix() and a lambda show two other bulk changes.add_prefix() helps before joining tables that share names, and any function works in rename(columns=...); see lambda functions in Python if that syntax is new.
Step 5: Set Column Names After a groupby Aggregation
A dictionary of function lists in agg() gives two-level column names, which are awkward to select:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
df = pd.read_csv("employees.csv")
out = df.groupby("Department").agg({"Annual Salary ($)": ["mean", "max"]})
print(out.columns.tolist())
out.columns = ["salary_mean", "salary_max"]
print(out)
Output:
[('Annual Salary ($)', 'mean'), ('Annual Salary ($)', 'max')]
salary_mean salary_max
Department
Engineering 85275.0 92300.0
Finance 69900.0 69900.0
Sales 93125.0 101750.0
Assigning a flat list replaces the (column, function) tuples. Named aggregation sets the names inside agg() itself; my pandas groupby aggregation guide shows that pattern.
pandas Column Names Cheat Sheet
| Task | Code | Notes |
|---|---|---|
| Name columns while reading | read_csv(f, names=cols, header=0) | header=None if the file has no header |
| Name columns while creating | pd.DataFrame(rows, columns=cols) | Dict keys also become names |
| Replace every name | df.columns = cols | Length must match |
| Replace every name in a chain | df.set_axis(cols, axis=1) | Returns a new DataFrame |
| Rename a few columns | df.rename(columns={...}) | Add errors="raise" to catch typos |
| Clean all names | df.columns.str.strip().str.lower() | Chain more string methods |
| Add a prefix or suffix | df.add_prefix("hr_") | add_suffix() for the end |
| Reorder while renaming | df[["b", "a"]].set_axis(["x", "y"], axis=1) | Select first, then name |
Common Mistakes to Avoid When Setting Column Names
Here are the three problems we reproduced most often:
import pandas as pd
pd.set_option("display.max_columns", None)
pd.set_option("display.width", 100)
df = pd.read_csv("employees.csv")
try:
df.columns = ["emp_id", "first_name", "last_name"]
except ValueError as e:
print("ValueError:", e)
same = df.rename(columns={"Hire date": "hired"})
print("Hire Date" in same.columns, "hired" in same.columns)
try:
df.rename(columns={"Hire date": "hired"}, errors="raise")
except KeyError as e:
print("KeyError:", e)
Output:
ValueError: Length mismatch: Expected axis has 7 elements, new values have 3 elements
True False
KeyError: "['Hire date'] not found in axis"
