In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the apply function in python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the apply function in python.
Function prototype:
DataFrame.apply (func, axis=0, broadcast=False, raw=False, reduce=None, args= (), * * kwds)
1. The most useful thing about this function is the first argument, which is the function, which is equivalent to the function pointer of Cstroke +.
two。 This function needs to be implemented by itself. The input parameters of the function are determined according to axis. For example, if axis = 1, a row of data will be taken as the data of Series.
If the structure is passed into the function implemented by ourselves, we implement the calculation of different attributes of Series in the function and return a result, then the apply function
Automatically traverses each row of DataFrame data, and finally combines all the results into a Series data structure
And return.
The 3.apply function is often used with the groupby function, as shown in the following figure:
4. Lift the chestnut
Operate on the specified column:
Data=np.arange (0meme 16). Reshape (4jue 4) data=pd.DataFrame (data,columns= ['0prit]]) def f (x): return x-1print (data) print (data.ix [:, [' 1pr]] .apply (f)) 0 12 30 0 12 31 4 5 6 72 8 10 113 12 13 14 15 1 20 11 4 52 8 93 12 13
Line matching operation:
Data=np.arange (0meme 16). Reshape (4jue 4) data=pd.DataFrame (data,columns= ['0prit] def f (x): return x-1print (data) print (data.ix [[0meme 1],:] .apply (f)) 0 12 30 0 12 31 4 6 72 8 10 113 12 13 14 0 12 30-10 1 21 3 4 5 6
Overall alignment operation:
Data=pd.DataFrame (data,columns=) def f (x): return x.max () print (data) print (data.apply (f)) 0 12 300 12 31 4 5 6 72 8 10 113 12 13 14 150 121 132 143 15dtype: int64
Overall alignment operation:
Data=np.arange (0meme 16). Reshape (4) data=pd.DataFrame (data,columns=) def f (x): return x.max () print (data) print (data.apply (fmae axistem 1)) 0 12 30 0 12 31 4 5 6 72 8 10 113 12 13 14 150 31 72 13 14 150 31 72 15dtype: int64 so far I believe you have a deeper understanding of "how to use the apply function in python", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 0
*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.