In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use the apply function in Pandas. It is very detailed and has a certain reference value. Friends who are interested must finish it!
Use apply to split text
The apply function in Pandas is widely used. Today I would like to share with you a trick to use apply to split the list columns in dataframe into multiple columns.
The data before the split is shown in red in the following figure:
After the split, as shown in the following figure:
In this case, Lemon uses data from akshare, and before starting, introduce the relevant package:
#-*-coding: utf-8-*-"@ Author: Lemon @ product: Python data @ Homepage: liyangbit.com" import numpy as np import pandas as pd import akshare as ak "
The version information of several Python libraries used by Lemon is as follows:
Print ('numpy version: {}' .format (np.__version__)) print ('pandas version: {}' .format (pd.__version__)) print ('akshare version: {}' .format (ak.__version__)) # numpy version: 1.18.1 # pandas version: 1.0.3 # akshare version: 0.7.53
If there is a problem with the running of the code, please check that the versions of these Python libraries are consistent with those above.
First, obtain the required data from akshare, which is divided into two steps. The first step is to obtain the list of fund codes, as follows:
Df = ak.fund_em_fund_name () .head (20) .tail (5) dfdf = df [['fund code', 'fund abbreviation']] print (df)
The second step is to obtain the fund net value data and the net value date, which is obtained through a custom function as follows:
# case where the custom function has only one parameter # obtain fund unit net value and net value date def get_mutual_fund (code): df = ak.fund_em_open_fund_info (fund=code, indicator= "unit net worth trend") dfdf = df [['net value date', 'unit net value', 'daily growth rate']] # df.columns = ['net value date', 'unit net worth' 'equityReturn',' unitMoney'] df ['net worth date'] = pd.to_datetime (df ['net worth date']) dfdf = df.sort_values ('net worth date', ascending=False) unit_equity = df.head (1) ['unit net worth'] .values [0] date_latest = df.head (1) ['net worth date'] .values [0] return [unit_equity, date_latest]
For this custom function, use apply to apply the custom function in pandas, which is a common way to use apply, as follows:
# date of obtaining the latest unit net value and net worth of the fund df ['tmp'] = df [' fund code'] .apply (get_mutual_fund) print (df)
The screenshot of the obtained data is as follows:
Text split
The tmp column in the figure above is the object we need to deal with this time.
There are many ways to deal with it. Here Lemon uses apply in pandas to deal with it, which is relatively convenient.
In the apply function, we can achieve our goal by using pd.Series.
# list unit net worth and net worth date separately df [['latest unit net worth', 'net worth date']] = df ['tmp'] .apply (pd.Series) dfdf = df.drop (' tmp',axis=1) print (df)
The results are as follows:
The above is all the contents of the article "how to use the apply function in Pandas". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 216
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.