AI Reading
Quick summary of this article
Renaming columns and indexes in Pandas makes data easier to read and work with. The two main methods are using the rename() function for specific changes, or directly assigning new names to all columns or indexes at once.
rename()lets you change specific column or index labels using a dictionary, likedf.rename(columns={'Old': 'New'}).- You can also rename index labels with
rename(), for exampledf.rename(index={0: 'First'}). - Direct assignment is simpler for renaming all columns at once, using
df.columns = ['New1', 'New2']. - Both methods improve data clarity and make analysis workflows easier to follow.
🔧 Data Cleaning and Transformation: Renaming Columns and Index in Pandas
🔍 Introduction
Renaming columns and indexes is an essential step in data cleaning, helping to improve readability and consistency in data analysis. Pandas offers flexible methods for renaming columns and indexes in DataFrames.
Key methods for renaming in Pandas include:
- 📌
rename() – Rename specific columns or index labels. - 📌 Direct Assignment – Assign new column or index names directly.
Let’s explore these methods with practical examples.
📌 Example 1: Using rename() to Rename Columns
import pandas as pd
# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob'], 'Score': [85, 90]}
df = pd.DataFrame(data)
# Renaming 'Name' to 'Student_Name' and 'Score' to 'Marks'
df = df.rename(columns={'Name': 'Student_Name', 'Score': 'Marks'})
print(df)
✅ Output:
Student_Name Marks
0 Alice 85
1 Bob 90
📌 Example 2: Using rename() to Rename Index
# Renaming index labels
df = df.rename(index={0: 'First', 1: 'Second'})
print(df)
✅ Output:
Student_Name Marks
First Alice 85
Second Bob 90
📌 Example 3: Direct Assignment for Renaming Columns
# Directly assigning new column names
df.columns = ['Name', 'Score']
print(df)
✅ Output:
Name Score
First Alice 85
Second Bob 90
🔖 Summary
🔹 rename() is flexible for renaming specific columns or indexes. 🔹 Direct Assignment is straightforward for renaming all columns or indexes.
Utilizing these methods enhances data clarity and simplifies analysis workflows. 🚀
