[989] How to Use the Apply Method in Pandas
References:
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=0
oraxis='index'
: apply function to each column, which is the default value.
axis=1
oraxis='column'
: apply function to each row, which is similar to Series.apply()
. This is more common to use.
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)]
An Example:
Copy values from column“y”to column“x”, if the value of column“y”is True.
You can achieve this using the apply
function in pandas. Assuming you have a DataFrame with columns “x” and “y”, you can do the following:
import pandas as pd
# Example DataFrame
data = {'x': [10, 20, 30],
'y': [True, False, True]}
df = pd.DataFrame(data)
# Apply the condition: if 'y' is True, assign the value to 'x'
df['x'] = df.apply(lambda row: row['y'] if row['y'] else row['x'], axis=1)
# An alternative way
df.x = df.apply(lambda row: row.y if row.y else row.x, axis=1)
print(df)
This will update the values in column “x” based on the condition in column “y”. If “y” is True, the value from “y” will be assigned to “x”; otherwise, the original value in “x” remains unchanged. Feel free to adapt this code snippet to your specific use case! 😊