Pandas three major tools - map, apply, applymap

50eed83e34f27e4791ae6db0e74045df.png

In actual work, when we use  pandasdata processing, we often process a single row, multiple rows (columns are also applicable) or even the entire data in the data frame in the same way, such as replacing the  sexfields  in the data with males . to 1, and female to 0 .

At this point, it's easy to think of  forloops. Using a  forloop is a very simple and straightforward way, but it is very inefficient. This article introduces the  pandasthree major tools in:  map、apply、applymap to solve the same needs as above.

  • map

  • apply

  • applymap

—  01 

Simulation data

Through a simulated data to illustrate the use of the three functions, in this example learned how to generate various simulated data. Data are as follows:

import pandas as pd
import numpy as np
boolean = [True, False]
gender = ["男","女"]
color = ["white","black","red"]
# 好好学习如何生成模拟数据:非常棒的例子
# 学会使用random模块中的randint方法
df = pd.DataFrame({"height":np.random.randint(160,190,100),
                     "weight":np.random.randint(60,90,100),
                     "smoker":[boolean[x] for x in np.random.randint(0,2,100)],
                     "gender":[gender[x] for x in np.random.randint(0,2,100)],
                     "age":np.random.randint(20,60,100),
                     "color":[color[x] for x in np.random.randint(0,len(color),100)]
                    })
df.head()

e762c33640690dbae97020ca73d7d50b.png

—  02 

map

demo

map()  will map the specified sequence according to the provided function.

The first argument function calls the function function with each element in the argument sequence, returning a new list containing the return value of each function function .

map(function, iterable)

af9be52ad6a8882e3258d1c34c33eecc.png

The actual data

Change male to 1 and female to 0 in gender

# 方式1:通过字典映射实现
dic = {"男":1, "女":0}  # 通过字典映射
df1 = df.copy()   # 副本,不破坏原来的数据df
df1["gender"] = df1["gender"].map(dic)
df1
# 方式2:通过函数实现
def map_gender(x):
    gender = 1 if x == "男" else 0
    return gender
df2 = df.copy()
# 将df["gender"]这个S型数据中的每个数值传进去
df2["gender"] = df2["gender"].map(map_gender)
df2

332b0f0bbfbecdcff974199a3207a25f.png

—  03 

apply

applyThe working principle of the  mapmethod is similar to that of the method, the difference is that  applyit can pass in functions with more complex functions, which can be said  applyto be  mapan advanced version

The functions of pandas  apply() can act on  Series either the entire or the entire  DataFrame, and the function is to automatically traverse the entire  Series or  DataFrame, run the specified function on each element.

In  DataFramemost methods of the object, there will be  axisthis parameter, which controls whether the operation you specify is along the 0 axis or the 1 axis. axis=0On behalf of the operation pair  列columnson axis=1behalf of the operation pair 行row

demo

  1. In the above data, subtract 3 from the value of the age field, that is, add -3

def apply_age(x,bias):
    return x + bias
df4 = df.copy()
# df4["age"]当做第一个值传给apply_age函数,args是第二个参数
df4["age"] = df4["age"].apply(apply_age,args=(-3,))

c5a02dcfa2e07777c5d4c085abcc5497.png

  1. Calculate BMI

# 实现计算BMI指数:体重/身高的平方(kg/m^2)
def BMI(x):
    weight = x["weight"]
    height = x["height"] / 100
    BMI = weight / (height **2)
    return BMI
df5 = df.copy()
df5["BMI"] = df5.apply(BMI,axis=1)  # df5现在就相当于BMI函数中的参数x;axis=1表示在列上操作
df5

33bf93f628cec0fa9195c44ef2d3dfa8.png

DataFrameapplyOperation summary of type data  :

  1. At that time axis=0 , the  每列columnsspecified function is executed; axis=1at that time  , the 每行rowspecified function is executed.

  2. Either  way, the default form of passing in the specified function is  axis=0that  it can be passed  in by setting  .axis=1Seriesraw=Truenumpy数组

  3. After executing the results of each Series, the results will be integrated and returned (if you want to have a return value, you need  returnthe corresponding value when defining the function)

apply implementation requirements

The above gender conversion requirements are realized through the apply method. The first parameter passed in the apply method must be a function

de6f88b50250583f647e928846135787.png

—  04 

applymap

DF data plus 1

The applymap function is used to perform the same functional operation on each element in the DF type data, such as the following plus 1:

cfcea73c4588d598fe32fb7672942035.png

Keep 2 significant digits

5f82b744caa53a31baf5136e9440fdb3.png

推荐阅读:
入门: 最全的零基础学Python的问题  | 零基础学了8个月的Python  | 实战项目 |学Python就是这条捷径
干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 |   从万众期待到口碑扑街!唐探3令人失望  | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影
趣味:弹球游戏  | 九宫格  | 漂亮的花 | 两百行Python《天天酷跑》游戏!
AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影
小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!|  再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|

The year's hottest copy

Click to read the original text to see 200 Python cases!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326842949&siteId=291194637