Introduction

Python’s Pandas library provides several methods for applying functions to data in DataFrames: apply, map, and applymap.

Creating a DataFrame

Let’s start by creating a sample DataFrame to work with.

# Import Pandas library
import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
    'City': ['New York', 'Los Angeles', 'Boston', 'Houston', 'Phoenix'],
    'Age': [25, 30, 35, 40, 45],
    'Score': [85, 90, 87, 92, 88]
}
df = pd.DataFrame(data)
df
##       Name         City  Age  Score
## 0    Alice     New York   25     85
## 1      Bob  Los Angeles   30     90
## 2  Charlie       Boston   35     87
## 3    David      Houston   40     92
## 4      Eve      Phoenix   45     88

Transforming Data

Using apply

The apply method can be used to apply a function along the axis of the DataFrame. This means that you can apply a function to each column or each row.

Applying a Function to Columns

Let’s add 5 to each score in the ‘Score’ column.

# Define a function to add 5 to a number
def add_five(x):
    return x + 5

# Apply the function to the 'Score' column
df['Score'] = df['Score'].apply(add_five)
df
##       Name         City  Age  Score
## 0    Alice     New York   25     90
## 1      Bob  Los Angeles   30     95
## 2  Charlie       Boston   35     92
## 3    David      Houston   40     97
## 4      Eve      Phoenix   45     93

Applying a Function to Rows

Now, let’s create a column ‘Senior’ that indicates if a person is 40 or older.

# Define a function to check if age is 40 or more
def is_senior(row):
    return row['Age'] >= 40

# Apply the function to each row
df['Senior'] = df.apply(is_senior, axis=1)
df
##       Name         City  Age  Score  Senior
## 0    Alice     New York   25     90   False
## 1      Bob  Los Angeles   30     95   False
## 2  Charlie       Boston   35     92   False
## 3    David      Houston   40     97    True
## 4      Eve      Phoenix   45     93    True

Using map

The map method is used to substitute each value in a Series with another value. It is only used for element-wise substitution in a Series (column).

Let’s replace the boolean values in ‘Senior’ column with ‘Yes’ and ‘No’.

# Replace boolean values with 'Yes' and 'No'
df['Senior'] = df['Senior'].map({True: 'Yes', False: 'No'})
df
##       Name         City  Age  Score Senior
## 0    Alice     New York   25     90     No
## 1      Bob  Los Angeles   30     95     No
## 2  Charlie       Boston   35     92     No
## 3    David      Houston   40     97    Yes
## 4      Eve      Phoenix   45     93    Yes

Using applymap

The applymap method is used to apply a function to each element of the DataFrame. This is useful when you want to perform element-wise operations.

Let’s convert all the strings in the DataFrame to uppercase.

# Convert strings to uppercase
def to_uppercase(x):
    if isinstance(x, str):
        return x.upper()
    return x

df = df.applymap(to_uppercase)
df
##       Name         City  Age  Score Senior
## 0    ALICE     NEW YORK   25     90     NO
## 1      BOB  LOS ANGELES   30     95     NO
## 2  CHARLIE       BOSTON   35     92     NO
## 3    DAVID      HOUSTON   40     97    YES
## 4      EVE      PHOENIX   45     93    YES

Summary