Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply apply and lambda in pandas

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to apply apply and lambda in pandas", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to apply apply and lambda in pandas" article.

The apply (func [, args [, kwargs]]) function is used to call a function indirectly when its arguments already exist in a tuple or dictionary. An args is a tuple that contains positional arguments to be supplied to a function. If args is omitted, none of the parameters will be passed, and kwargs is a dictionary that contains keyword arguments. To put it simply, the return value of apply () is the return value of func (). The element parameters of apply () are ordered, and the order of the elements must be the same as the order of the parameters in the form of func (). The difference from map is that the former is for column and the latter is for elements.

Lambda is an anonymous function, that is, it no longer uses the form of def, which can simplify the script and make the structure non-redundant and concise.

A = lambda x: X + 1 a (10) 11

The combination of the two can do a lot of things. For example, many functions of split are not available in series, but index can do it.

For example, if you have a string of data as follows, which should be divided into totals, correct numbers, and correct rates, you can do so.

96% (1368608 Compact 1412722)

97% (1389916 Universe 1427922)

97% (1338695 Compact 1373803)

96% (1691941Compact 1745196)

95% (1878802 Compact 1971608)

97% (944218max 845)

96% (1294939amp 1336576)

Mr. import pandas as pd# becomes a dataframed = {"col1": ["96% (1368608max 1412722)", "97% (1389916max 1427922)", "97% (1338695 ash 1373803)", "96% (1691941max 1745196)", "95% (1878802max 1971608)", "97% (944218max 845)", "96% (1294939ama 1336576)"} df1 = pd.DataFrame (d) # Total recognition rate in the original text Using apply + anonymous function # lambda function means to select the sequence value of x For example, the x [6:9] # index function means to convert the current character position to the number of digits in it. #-1 is the last bit df1 ['correct number'] = df1.iloc [:, 0] .apply (lambda x: X [x.index ('() + 1: x.index ('/')]) df1 ['Total'] = df1.iloc [: 0] .apply (lambda x: X [x.index ('/') + 1:-1]) df1 ['accuracy'] = df1.iloc [:, 0] .apply (lambda x: X [: x.index ('()]) df1

Example 2

From a set of dataframe data, including three columns of numerical meteorological elements, the human comfort index is calculated by formula.

Applied to the calculation formula of human comfort index:

Import pandas as pdimport numpy as npimport mathpath='D:\\ data\\ 57582.csv' # File path data=pd.read_csv (path,index_col=0,encoding='gbk') # use gbk decoding when reading data in Chinese # define comfort index formula function Results retain 1 decimal place def get_CHB (Tgrame RHMague S): return round (1.8 percent TMEC 0.55 * (1.8*T-26) * (1-RH/100)-3.2*math.sqrt (S) + 32) # add a column of CHB and calculate the data and assign the value data ['comfort index'] = data.apply (lambda x:get_CHB (x ['average temperature'], x ['average relative humidity'], x ['2m wind speed]) Axis=1) # print result print (data) # Save result data.to_csv ('D:\\ CHB.csv',encoding='gbk')

The combination of apply and lambda is used in the code. The parameter x passed in is the entire data data, and the parameters introduced in the function are x ['average temperature'], x ['average relative humidity'], x ['2m wind speed'], corresponding to the custom function get_CHB. Finally, you need to use axis=1 to specify whether to operate on the column.

The result is shown in the figure:

The above is about the content of this article on "how to apply apply and lambda in pandas". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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: 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report