In actual work, when we use pandas
data 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 sex
fields in the data with males . to 1, and female to 0 .
At this point, it's easy to think of for
loops. Using a for
loop is a very simple and straightforward way, but it is very inefficient. This article introduces the pandas
three 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()
— 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)
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
— 03 —
apply
apply
The working principle of the map
method is similar to that of the method, the difference is that apply
it can pass in functions with more complex functions, which can be said apply
to be map
an 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 DataFrame
most methods of the object, there will be axis
this parameter, which controls whether the operation you specify is along the 0 axis or the 1 axis. axis=0
On behalf of the operation pair 列columns
, on axis=1
behalf of the operation pair 行row
demo
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,))
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
DataFrame
apply
Operation summary of type data :
At that time
axis=0
, the每列columns
specified function is executed;axis=1
at that time , the每行row
specified function is executed.Either way, the default form of passing in the specified function is
axis=0
that it can be passed in by setting .axis=1
Series
raw=True
numpy数组
After executing the results of each Series, the results will be integrated and returned (if you want to have a return value, you need
return
the 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
— 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:
Keep 2 significant digits
推荐阅读:
入门: 最全的零基础学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
5). 20 python codes you must master, short and powerful, infinitely useful
7). The 80 pages I summarized in the "Rookie Learning Python Selected Dry Goods.pdf" are all dry goods
8). Goodbye Python! I'm going to learn Go! 2500 words in-depth analysis !
Click to read the original text to see 200 Python cases!