alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[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=0oraxis='index': apply function to each column, which is the default value.

axis=1oraxis='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! 😊

posted on   McDelfino  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示