0
0
Data Analysis Pythondata~5 mins

Renaming columns in Data Analysis Python

Choose your learning style9 modes available
Introduction

Renaming columns helps make data easier to understand and work with. It gives clear names that describe the data better.

When column names are unclear or too long.
When you want to standardize column names across different datasets.
When preparing data for reports or presentations.
When fixing typos or inconsistent naming in columns.
When merging datasets that have different column names for the same data.
Syntax
Data Analysis Python
df.rename(columns={'old_name': 'new_name', ...}, inplace=False)

Use a dictionary to map old column names to new ones.

Set inplace=True to change the original DataFrame directly.

Examples
Rename the column 'Age' to 'age_years'. This returns a new DataFrame.
Data Analysis Python
df.rename(columns={'Age': 'age_years'})
Rename multiple columns and update the original DataFrame.
Data Analysis Python
df.rename(columns={'Name': 'Full Name', 'Salary': 'Monthly Salary'}, inplace=True)
Create a new DataFrame with renamed columns without changing the original.
Data Analysis Python
new_df = df.rename(columns={'Old': 'New'})
Sample Program

This code creates a simple table with names, ages, and salaries. Then it renames 'Name' to 'Full Name' and 'Salary' to 'Monthly Salary'. It shows the original and renamed tables.

Data Analysis Python
import pandas as pd

# Create a sample DataFrame
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30], 'Salary': [50000, 60000]}
df = pd.DataFrame(data)

print('Original DataFrame:')
print(df)

# Rename columns
renamed_df = df.rename(columns={'Name': 'Full Name', 'Salary': 'Monthly Salary'})

print('\nDataFrame after renaming columns:')
print(renamed_df)
OutputSuccess
Important Notes

If you forget inplace=True, the original DataFrame stays the same.

You can rename one or many columns at once using the dictionary.

Renaming does not change the data, only the column labels.

Summary

Renaming columns makes data easier to read and use.

Use df.rename(columns={...}) with a dictionary of old and new names.

Remember to use inplace=True if you want to change the original DataFrame.