Hello everyone , I am a Get up early , Today I'd like to share with you pandas in apply A trick of functions .
Pandas in apply function , Widely applied , Today I want to share with you a skill of using , Use apply take dataframe The content of list Split the columns of into multiple columns .
Before splitting Data of , As shown in the red mark below ：
After break up , As shown in the figure below ：
In this case ,Lemon The data used comes from
akshare , Before the start , To introduce the relevant package ：
import pandas as pd import akshare as ak
Lemon Two used Python The version information of the library is as follows ：
print(ak.__version__) 0.7.22 print(pd.__version__) 1.1.4
First from akshare Get the data you need , Divided into two steps , First step It's a list of fund codes , as follows ：
df = ak.fund_em_fund_name().head(20).tail(5) df = df[[' Fund code ',' Fund abbreviation ']] print(df)
The second step Is to obtain the net value data and net value date of the fund , Get... Through a custom function , The custom functions are as follows ：
# Get the net value of the fund unit and the net value date def get_mutual_fund(code): df = ak.fund_em_open_fund_info(fund=code, indicator=" Unit net worth trend ") df.columns = [' Net value date ', ' Unit net worth ', 'equityReturn', 'unitMoney'] df[' Net value date '] = pd.to_datetime(df[' Net value date ']) df = df.sort_values(' Net value date ',ascending=False) unit_equity = df.head(1)[' Unit net worth '].values date_latest = df.head(1)[' Net value date '].values return [unit_equity, date_latest]
For this custom function , stay pandas Use apply To apply custom functions , This is the use of apply It's a common method of , as follows ：
# Get the latest unit net value and net value date of the fund df['tmp'] = df[' Fund code '].apply(get_mutual_fund) print(df)
The obtained data screenshot is as follows ：
In the picture above
tmp Column , That's what we need to deal with this time .
There are many ways to deal with it , here Lemon Use pandas Medium apply To deal with it , relatively speaking , It's also a convenient way .
stay apply Function , Use
pd.Series We can achieve our goal .
# Separate unit net value and net value date df[[' The latest net unit value ',' Net value date ']] = df['tmp'].apply(pd.Series) df = df.drop('tmp',axis=1) print(df)
give the result as follows ：
Seemingly complex problems , It's also very convenient to solve , Isn't it easy , Use it quickly .
This article is from WeChat official account. - Get up early Python（zaoqi-python）
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the email@example.com Delete .
Original publication time ： 2021-01-12
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .