Renaming Columns in Pandas
As datasets grow, column names often become unclear, too long, or inconsistent.
Renaming columns helps improve readability, maintain consistency, and avoid confusion in analysis.
Why Renaming Columns Is Important
Clean column names make your data:
- Easier to understand
- Faster to work with in code
- Consistent across teams and projects
- Ready for reporting and dashboards
Loading the Dataset
We continue using the same Pandas dataset used in previous lessons.
import pandas as pd
df = pd.read_csv("dataplexa_pandas_sales.csv")
Viewing Existing Column Names
Before renaming, always inspect current column names.
df.columns
This returns a list of all column names in the dataset.
Renaming a Single Column
To rename one column, use the
rename() method.
Example: Rename sales to total_sales.
df.rename(columns={"sales": "total_sales"}, inplace=True)
The change is applied immediately because
inplace=True is used.
Renaming Multiple Columns
You can rename several columns at once using a dictionary.
Example:
df.rename(columns={
"product": "product_name",
"region": "sales_region",
"date": "order_date"
}, inplace=True)
This approach is commonly used when cleaning raw datasets.
Renaming All Columns at Once
If you want to replace all column names, assign a new list directly.
Example:
df.columns = [
"order_id",
"order_date",
"product_name",
"sales_region",
"quantity",
"total_sales"
]
Make sure the number of new names matches the number of columns.
Standardizing Column Names
In real projects, column names should follow a consistent format.
Common best practices:
- Use lowercase letters
- Avoid spaces
- Use underscores instead of special characters
Example:
df.columns = df.columns.str.lower().str.replace(" ", "_")
This is extremely useful when working with messy datasets.
Renaming Columns for Reporting
Sometimes column names need to look clean for reports or exports.
Example:
df.rename(columns={
"total_sales": "Total Sales ($)",
"order_date": "Order Date"
}, inplace=True)
This improves readability for non-technical users.
Practice Exercise
Using the dataset:
- Print all column names
- Rename one column of your choice
- Rename at least three columns together
- Convert all column names to lowercase
What’s Next?
In the next lesson, you will learn how to add and remove columns to modify datasets dynamically.