Data Science Pandas

6.1. Data Cleaning and Transformation – Applying Functions on Data in Pandas

🔧 Data Cleaning and Transformation: Applying Functions on Data in Pandas

🔍 Introduction

Applying functions to data is a crucial step in data cleaning and transformation. Pandas offers powerful methods to apply functions across DataFrames and Series, enabling efficient and effective data processing.

Key methods for applying functions in Pandas include:

  1. 📌 apply() – Apply functions along rows or columns.
  2. 📌 map() – Apply functions element-wise on Series.
  3. 📌 applymap() – Apply functions element-wise across DataFrames.

In this tutorial, we will explore how to utilize these methods with practical examples.

📌 Example 1: Using apply() on DataFrame Columns

The apply() method can be used to apply custom or built-in functions along the columns or rows of a DataFrame.

import pandas as pd

# Creating a sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Score': [85, 90, 95]}
df = pd.DataFrame(data)

# Applying a custom function to increment scores by 5
df['Adjusted_Score'] = df['Score'].apply(lambda x: x + 5)
print(df)

✅ Output:

      Name  Score  Adjusted_Score
0   Alice     85              90
1     Bob     90              95
2  Charlie     95             100

📌 Example 2: Using map() for Series Transformations

The map() function is useful for element-wise transformations of a Pandas Series.

# Converting names to uppercase using map()
df['Name'] = df['Name'].map(str.upper)
print(df)

✅ Output:

      Name  Score  Adjusted_Score
0   ALICE     85              90
1     BOB     90              95
2  CHARLIE     95            100

📌 Example 3: Using applymap() for Element-wise DataFrame Operations

The applymap() method applies functions to each element of the DataFrame.

# Creating a numeric DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df_num = pd.DataFrame(data)

# Squaring each element
df_squared = df_num.applymap(lambda x: x ** 2)
print(df_squared)

✅ Output:

    A   B
0   1  16
1   4  25
2   9  36

🔖 Summary

🔹 apply() is ideal for applying functions across DataFrame rows or columns. 🔹 map() is efficient for element-wise operations on Series. 🔹 applymap() is used for element-wise operations on entire DataFrames.

Choosing the right method simplifies data cleaning and transformation processes, ensuring cleaner and more consistent datasets for analysis. 🚀

Leave a Reply

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