2021-07-29
Source: AI introductory learning
Author: Xiao Wu Ge
The map in pandas is similar to the built-in map() method in Python. The map() method in pandas connects functions, dictionary indexes, or some special objects that need to accept a single input value with each element of the corresponding single column. And get the result serially.
Here we want to get the new columns of gender column F and M converted to female and male. There are several ways to implement a data set first.
The map() function can be used for a column of a Series object or a DataFrame object. It receives a function or a dictionary object as a parameter, and returns the value after the function or dictionary mapping process.
Usage: Series .map(arg, na_action=None)
parameter:
arg : function, dict, or Series
Mapping correspondence.
na_action : {None,'ignore'}, default None
If'ignore', propagate NaN values, without passing them to the mapping
correspondence.
Return: Pandas Series with same as index as caller
Official: https:// pandas .pydata.org/ pandas -docs/stable/reference/api/ pandas . Series .map.html
First build a data set, the following case application
data = pd. DataFrame ( {"name":['Jack', 'Alice' , 'Lily' ,
'Mshis' , 'Gdli' , 'Agosh' , 'Filu' , 'Mack' , 'Lucy' , 'Pony' ],"gender" :['F', 'M' , 'F' , 'F' , 'M' , 'F' , 'M' , 'M' , 'F' , 'F' ],"age" :[25, 34 , 49 , 42 , 28 , 23 , 45 , 21 , 34 , 29 ]} )data name gender age 0 Jack F 25 1 Alice M 34 2Lily F 49 3 Mshis F 42 4 Gdli M 28 5 Agosh F 23 6Filu M 45 7 Mack M 21 8 Lucy F 34 9 Pony F 291 Dictionary mapping
Here we write a dictionary of one-to-one mapping between F, M and female and male, and then use the map() method to get the mapping column:
#Define F-gt;female, M-gt; male mapping dictionary gender2xb = {'F':'female','M':'male'}#Using the map() method to get the mapping column corresponding to the gender column data.gender.map(gender2xb)0 female 1 male 2 female 3 female 4 male 5 female 6 male 7 male 8 female 9 female
2 lambda function
Here we pass in the lambda function to map() to achieve the required functions:
#Because we already know that there are only F and M in the gender of the data gender column, write the following lambda function
data.gender.map (lambda x: 'Women' if x == 'F' else ' male')0 female 1 male 2 female 3 female 4 male 5 female 6 male 7 male 8 female 9 female#Age's square data.age.map(lambda x: x**2)0 625 1 1156 2 2401 3 1764 4 784 5 529 6 2025 7 441 8 1156 9 843 General functions
The map function, you can also pass in the regular function defined by def, take a look at the following case
# Gender transition def gender_to_xb (x): return 'female' if x == 'F' else ' male'data.gender.map(gender_to_xb) 0 female 1 male 2 female3 female 4 male 5 female 6 male 7 male 8 female 9 female4 Special objects
The content that map() can pass in can sometimes be very special, as in the following example: some objects that receive a single input value and have an output can also be processed by the map() method:
data.gender. map ( "This kid's gender is {}" .format) 0
This kid's gender is F 1 This kid's gender is M 2
This kid's gender is F 3 This kid's gender is F 4
This kid's gender is M 5 This kid's gender is F 6
This kid's gender is M 7 This kid's gender is M 8
This kid's gender is F 9 This kid's gender is FThe parameter na_action in map() is similar to na.action in R. The value is None or ingore. It is used to control the processing method for missing values. When set to ingore, the Nan value will be ignored during the serial operation and returned as it is.
s = pd. Series (['cat','dog', np.nan,'rabbit']) s 0 cat 1 dog 2 NaN 3 rabbit
When na_action is the default value
s.map('I am a {}'.format) 0 I am a cat 1 I am a dog 2 I am a nan 3 I am a rabbit
When na_action is ignore
s.map('I am a {}'.format, na_action='ignore')0 I am a cat1 I am a dog2 NaN3 I am a rabbit
Thanks for watching
CDA Certification
About CDA Exam Latest Exam Schedule Becoming CDA MemberCDA Cooperation
CDA Education CDMS Pearson CVA InstituteFollow CDA
About US Email:exam@cdaglobal.com Tel:010-68454276