Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the apply function in pandas to apply a function with two parameters

Shulou Source: shulou.com Published: 2022-06-01 04:51:54 09月20日 Update

This article is about how to use the apply function in pandas to apply functions with two arguments. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

Functions with two parameters

The apply function in pandas When applying custom functions, usually there are no parameters or one parameter, so if there are two parameters, can I still use the apply function?

The answer is yes.

Here we also discuss.

Again, based on the above example, the same data is obtained from akshare first.

df1 = ak.fund_em_fund_name().head(20).tail(5) df1df1 = df1["'Fund code',"'Fund shorthand ']]

Next, customize a function with two parameters as follows:

#Custom function has two parameters #Get year-end fund net worth def get_mutual_fund_year(code,year): year = str(year) df = ak.fund_em_open_fund_info(fund=code, indicator="unit net worth trend") dfdf = df["'Net date','Unit net',' Daily growth']] # df.columns = ['Net date',' Unit net','equityReturn',' unitMoney'] df <$'Net Date']= pd.to_datetime(df <$'Net Date']) dfdf = df.sort_values ('net date', ascending=False) dfdf = df.set_index ('net date')[year] dfdf = df.reset_index() unit_equity = df.head(1)['unit net'].values[0] date = df.head(1)['net date'].values[0] return [unit_equity,date]

Custom functions with two parameters

Then, use apply to apply the above custom function with two parameters. The core point is to nest lambda functions and fix one of the parameters, as follows

df1 <$'tmp '] = df1 <$'fund code'].apply(lambda code: get_mutual_fund_year(code, 2019))

Next, it is still text splitting, and the implementation code is as follows:

#Separate Unit Net and Net Date columns df1[['Latest Unit Net','Net Date']] = df1['tmp'].apply(pd.Series) df1df1 = df1.drop ('tmp ',axis=1) print(df1)

application scenarios

Some students may ask, what is the use of a custom function with two parameters?

Here, Lemon also shares an application scenario:

According to the basic prototype data above, for a specific year, create a drop-down list. When selecting different years, return the results of different years, including text data, table data and charts.

The effects are as follows:

Thank you for reading! About "how to use apply function in pandas to apply function with two parameters" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Tags: Net worth function parameter two date application unit data fund code different content scenario base year year text more article prototype Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Apple Xiaomi MariaDB