Data Science Pandas

6.3.Data Cleaning and Transformation – Renaming Columns and Index in Pandas

🔧 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:

  1. 📌 rename() – Rename specific columns or index labels.
  2. 📌 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. 🚀

Leave a Reply

Your email address will not be published. Required fields are marked *