Pandas数据处理——map、apply、applymap的异同

在日常的数据处理中,经常会对一个DataFrame进行逐行、逐列和逐元素的操作,对应这些操作,Pandas中的map、apply和applymap可以解决绝大部分这样的数据处理需求。

import pandas as pd
import numpy as np

# 创建数据集
boolean = ['True','False']
gender = ['男','女']
color = ['white','black','green']
data = pd.DataFrame({
    'height':np.random.randint(150,190,100),
    'weight':np.random.randint(40,90,100),
    'smoker':[boolean[x] for x in np.random.randint(0,len(boolean),100)],
    'gender':[gender[x] for x in np.random.randint(0,len(gender),100)],
    'age':np.random.randint(15,90,100),
    'color':[color[x] for x in np.random.randint(0,len(color),100)]    
})
data.head()
height weight smoker gender age color
0 189 70 False 72 green
1 153 42 False 49 black
2 178 51 False 50 white
3 170 47 True 85 black
4 163 51 False 89 green

Series数据处理

1. map

如果需要把数据集中的gender列的男替换为1,女替换为0。应该怎么做呢?

# 使用字典映射
data['gender_x'] = data['gender'].map({'男':1,'女':0})
data.head()
height weight smoker gender age color gender_x
0 189 70 False 72 green 0
1 153 42 False 49 black 0
2 178 51 False 50 white 0
3 170 47 True 85 black 1
4 163 51 False 89 green 1
# 使用函数
def gender_map(x):
    gender = 1 if x =='男' else 0
    return gender
# 传入的函数名,不带括号
data['gender_x'] = data['gender'].map(gender_map)
data.head()
height weight smoker gender age color gender_x
0 189 70 False 72 green 0
1 153 42 False 49 black 0
2 178 51 False 50 white 0
3 170 47 True 85 black 1
4 163 51 False 89 green 1

不论是利用字典还是函数进行映射,map方法都是把对应的数据逐个当作参数传入到字典或函数中,得到映射后的值。

2. apply

同时Series对象还有apply方法,apply方法的作用原理和map方法类似,区别在于apply能够传入功能更为复杂的函数。怎么理解呢?一起看看下面的例子。

假设在数据统计的过程中,年龄age列有较大误差,需要对其进行调整(加上或减去一个值),由于这个加上或减去的值未知,故在定义函数时,需要加多一个参数bias,此时用map方法是操作不了的**(传入map的函数只能接收一个参数)**,apply方法则可以解决这个问题。

def age_apply(x,bias):
    return x+bias

# 额外的参数需要单独给到
data['age_x'] = data['age'].apply(age_apply,bias=-3)
data.head()
height weight smoker gender age color gender_x age_x
0 189 70 False 72 green 0 69
1 153 42 False 49 black 0 46
2 178 51 False 50 white 0 47
3 170 47 True 85 black 1 82
4 163 51 False 89 green 1 86

对于Series而言,map可以解决绝大多数的数据处理需求,但如果需要使用较为复杂的函数,则需要用到apply方法。

DataFrame数据

1. apply

对DataFrame而言,apply是非常重要的数据处理方法,它可以接收各种各样的函数(Python内置的或自定义的),处理方式很灵活,下面通过几个例子来看看apply的具体使用及其原理。

在进行具体介绍之前,首先需要介绍一下DataFrame中axis的概念,在DataFrame对象的大多数方法中,都会有axis这个参数,它控制了你指定的操作是沿着0轴还是1轴进行。axis=0代表操作对列columns进行,axis=1代表操作对行row进行。

假设现在需要对data中的数值列分别进行取对数和求和的操作,这时可以用apply进行相应的操作,因为是对列进行操作,所以需要指定axis=0

# 沿着0轴求和
df1 = data[['height','weight','age']].apply(np.sum,axis=0)
df1
height    16964
weight     6268
age        4728
dtype: int64
df2 = data[['height','weight','age']].apply(np.log,axis=0)
df2
height weight age
0 5.241747 4.248495 4.276666
1 5.030438 3.737670 3.891820
2 5.181784 3.931826 3.912023
3 5.135798 3.850148 4.442651
4 5.093750 3.931826 4.488636
... ... ... ...
95 5.056246 4.007333 4.430817
96 5.068904 4.343805 4.127134
97 5.170484 4.418841 4.356709
98 5.198497 4.304065 3.433987
99 5.220356 4.465908 3.178054

100 rows × 3 columns

当沿着轴0(axis=0)进行操作时,会将各列(columns)默认以Series的形式作为参数,传入到你指定的操作函数中,操作后合并并返回相应的结果。

在数据集中,有身高和体重的数据,所以根据这个,我们可以计算每个人的BMI指数(体检时常用的指标,衡量人体肥胖程度和是否健康的重要标准),计算公式是:体重指数BMI=体重/身高的平方(国际单位kg/㎡),因为需要对每个样本进行操作,这里使用axis=1的apply进行操作,代码如下:

def BMI(series):
    weight = series['weight']
    height = series['height']
    BMI = weight/height**2
    
    return BMI

data['BMI'] = data.apply(BMI,axis=1)
data.head()
height weight smoker gender age color gender_x age_x BMI
0 189 70 False 72 green 0 69 0.001960
1 153 42 False 49 black 0 46 0.001794
2 178 51 False 50 white 0 47 0.001610
3 170 47 True 85 black 1 82 0.001626
4 163 51 False 89 green 1 86 0.001920

总结一下对DataFrame的apply操作:

  1. 当axis=0时,对每列columns执行指定函数;当axis=1时,对每行row执行指定函数。
  2. 无论axis=0还是axis=1,其传入指定函数的默认形式均为Series,可以通过设置raw=True传入numpy数组。
  3. 对每个Series执行结果后,会将结果整合在一起返回(若想有返回值,定义函数时需要return相应的值)
  4. 当然,DataFrame的apply和Series的apply一样,也能接收更复杂的函数,如传入参数等,实现原理是一样的,具体用法详见官方文档。

2. applymap

applymap的用法比较简单,会对DataFrame中的每个单元格执行指定函数的操作。

# 构建一个新的数据集
data2 = pd.DataFrame({
    'A':np.random.randn(5),
    'B':np.random.randn(5),
    'C':np.random.randn(5),
    'D':np.random.randn(5)
})
data2
A B C D
0 0.319973 0.398420 -0.421453 -1.324070
1 0.507460 0.529772 0.650397 0.645287
2 0.659481 -0.858528 -1.400294 1.834288
3 1.265254 1.685537 2.031475 -2.136325
4 -2.616967 0.023655 -0.080487 -0.694068

现在想将DataFrame中所有的值保留两位小数显示,使用applymap即可。

data2.applymap(lambda x:"%.2f" % x)
A B C D
0 0.32 0.40 -0.42 -1.32
1 0.51 0.53 0.65 0.65
2 0.66 -0.86 -1.40 1.83
3 1.27 1.69 2.03 -2.14
4 -2.62 0.02 -0.08 -0.69
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章