In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of how to use Pandas's map,apply,applymap, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use map,apply,applymap in Pandas. Let's take a look at it.
Simulated data
Through a simulation data to illustrate the use of three functions, in this example learned how to generate a variety of simulation data. The data are as follows:
Import pandas as pdimport numpy as npboolean = [True, False] gender = ["male", "female"] color = ["white", "black", "red"] # learn how to generate simulation data well: a great example # learn to use the randint method in the random module df = pd.DataFrame ({"height": np.random.randint (160190100), "weight": np.random.randint (6090100)) "smoker": [Boolean [x] for x in np.random.randint], "gender": [Gender [x] for x in np.random.randint (0je 2100)], "age": np.random.randint (20m 60100), "color": [Colour [x] for x in np.random.randint (0Len (color)) ]}) df.head ()
1 、 mapdemo
Map () maps the specified sequence based on the function provided.
The first argument, function, calls the function function with each element in the argument sequence, returning a new list of values returned by each function function.
Map (function, iterable)
practical data
Change the male into 1 and the female into 0 in gender
# method 1: implement dic = {"male": 1, "female": 0} # through dictionary mapping df1 = df.copy () # copy Do not destroy the original data dfdf1 ["gender"] = df1 ["gender"] .map (dic) df1# method 2: implement def map_gender (x): gender = 1 if x = = "male" else 0 return genderdf2 = df.copy () # transfer each value in the S-shaped data df ["gender"] to df2 ["gender"] = df2 ["gender"] .map (map_gender) df2
2 、 apply
The working principle of apply method is similar to that of map method, except that apply can pass in functions with more complex functions. It can be said that apply is the advanced version of map.
The apply () function of pandas can act on Series or the entire DataFrame, and it also automatically traverses the entire Series or DataFrame, running the specified function on each element.
In most methods of the DataFrame object, there is the parameter axis, which controls whether the operation you specify is performed along the 0 axis or 1 axis. Axis=0 represents operation on column columns, and axis=1 represents operation on row row.
Demo
In the above data, subtract 3 from the value of the age field, that is, add-3
Def apply_age: return x + biasdf4 = df.copy () # df4 ["age"] is passed to the apply_age function as the first value, and args is the second parameter df4 ["age"] = df4 ["age"] .apply (apply_age,args= (- 3,))
Calculate BMI index
# calculate the BMI index: weight / height squared (kg/ m ^ 2) def BMI (x): weight = x ["weight"] height = x ["height"] / 100 BMI = weight / (height * * 2) return BMIdf5 = df.copy () df5 ["BMI"] = df5.apply (BMI,axis=1) # df5 is now equivalent to the parameter xtransferaxiseg1 in the BMI function to operate df5 on the column
Summary of apply operations for DataFrame data:
When axis=0, the specified function is executed for each column of columns; when axis=1, the specified function is executed for each row of row.
Whether axis=0 or axis=1, the default form of the specified function is Series, and you can pass in the numpy array by setting raw=True.
After executing the results for each Series, the results are integrated and returned (if you want to have a return value, you need the corresponding value of return when defining the function)
Apply implementation requirements
The above gender conversion needs are realized through the apply method. The first parameter passed in the apply method must be a function
3. Add 1 to applymapDF data
The applymap function is used to perform the same function operation on each element of DF data, such as the following plus 1:
Keep 2 significant digits
This is the end of the article on "how to use Pandas's map,apply,applymap". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Pandas's map,apply,applymap". If you want to learn more, you are 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: 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.