[989] How to Use the Apply Method in Pandas

alex_bn_lee / 2024-04-30 / 原文

References:

  • Tutorial: How to Use the Apply Method in Pandas
  • pandas.Series.apply
  • pandas.DataFrame.apply

1. pandas.Series.apply

Apply a function to each element of a Series. 

import pandas as pd

# Create a Series
s = pd.Series([1, 2, 3, 4, 5])

# Define a function
def square(x):
    return x ** 2

# Apply the function to each element of the Series
result = s.apply(square)
print(result)

or 

# Apply the lambda function to each element of the Series
result = s.apply(lambda x: x ** 2)

2. pandas.DataFrame.apply

Apply a function along an axis of the DataFrame.

axis=0oraxis='index': apply function to each column, which is the default value.

axis=0oraxis='column': apply function to each row, which is similar to Series.apply().

import pandas as pd

# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})

# Define a function
def sum_row(row):
    return row['A'] + row['B']

# Apply the function along the rows (axis=1)
result_row = df.apply(sum_row, axis=1)
print(result_row)

# Apply the function along the columns (axis=0)
result_col = df.apply(sum, axis=0)
print(result_col)

or

# Apply the lambda function along the rows (axis=1)
result_row = df.apply(lambda x: x['A'] + x['B'], axis=1)

In the DataFrame, we can get the column Series by this way: df.A and df.B, which are the same to df['A'] and df['B']

Then can use them as the Series.apply function.

Some examples:

# Series
data['FirstName'] = data['EmployeeName'].apply(lambda x : x.split()[0])
data['FirstName'] = data.EmployeeName.apply(lambda x : x.split()[0])
data['LastName'] = data['EmployeeName'].apply(lambda x : x.split()[1])
data['LastName'] = data.EmployeeName.apply(lambda x : x.split()[1])
data['HireDate'].apply(lambda x: date.today().year - x >= 10)
data.HireDate.apply(lambda x: date.today().year - x >= 10)

# DataFrame
data["BMI"] = data.apply(lambda x: round(x["Weight"] / (x["Height"] / 100) ** 2, 2), axis=1)
data[data.apply(lambda x: True if x['Gender'] == 'F' and x['Kids'] > 0 else False, axis=1)]
data[data.apply(lambda x: True if x.Gender == "F" and x.Kids > 0 else False, axis=1)]