1、apply()
介绍:应用于每个列或行的功能,默认每一列;在后面加 axis=1改为用于行
df = pd.DataFrame([[1,3]]*3)
df.columns = ['one','two']
print(df)
one two
0 1 3
1 1 3
2 1 3
1.1 生成新的一列
df['three'] = df.apply(lambda x:x['one']+x['two'], axis=1)
print(df)
one two three
0 1 3 4
1 1 3 4
2 1 3 4
1.2 对每列求和
print(df.apply(np.sum))
one 3
two 9
three 12
dtype: int64
2、map()
介绍:根据提供的函数对指定序列做映射。返回一个“对象”;可以用list出来
def square(x):return x ** 2
lst1 = list(map(square, [1,3,5,7,8]))
print(lst1)
[1, 9, 25, 49, 64]
lst2 = list(map(lambda x:x**2, [1,3,5,7,8]))
print(lst2)
[1, 9, 25, 49, 64]
lst3 = list(map(lambda x,y:x+y,[1,2,3,4,5],[6,7,8,9,0]))
print(lst3)
[7, 9, 11, 13, 5]
3、applymap()
介绍:应用于每个值,用于元素操作。
import pandas as pd
df = pd.DataFrame([[1,2.632],[4.234, 0.234]],columns=['first', 'second'])
print(df)
first second
0 1.000 2.632
1 4.234 0.234
df.applymap(lambda x:int(x))
first second
0 1 2
1 4 0
df.applymap(lambda x:(str(x)))
first second
0 1.0 2.632
1 4.234 0.234
20201122更新补充一些实例:
首先需要介绍一下DataFrame中axis的概念,
在DataFrame对象的大多数方法中,都会有axis这个参数,它控制了你指定的操作是沿着0轴还是1轴进行。
axis=0代表操作对列columns进行,axis=1代表操作对行row进行
1、map() 应用于每个列或行的功能
创建测试数据源
import numpy as np
import pandas as pd
boolean=[True,False]
gender=[“男”,“女”]
color=[“white”,“black”,“yellow”]
data=pd.DataFrame({
“height”:np.random.randint(150,190,6),
“weight”:np.random.randint(40,90,6),
“smoker”:[boolean[x] for x in np.random.randint(0,2,6)],
“gender”:[gender[x] for x in np.random.randint(0,2,6)],
“age”:np.random.randint(15,90,6),
“color”:[color[x] for x in np.random.randint(0,len(color),6) ]
})
data
height weight smoker gender age color
0 161 54 False 男 87 white
1 173 78 False 女 77 yellow
2 185 50 True 男 65 white
3 167 65 True 女 49 yellow
4 158 54 True 女 83 white
5 181 56 True 男 32 yellow
map(),传入字典
data[‘gender_1’]=data[‘gender’].map({‘男’:1,‘女’:0})
data[[‘gender’,‘gender_1’]]
gender gender_2
0 男 1
1 女 0
2 男 1
3 女 0
4 女 0
5 男 1
map(),传入函数
data[‘gender_2’] = data[‘gender’].map(lambda x:1 if x == “男” else 0)
data[[‘gender’,‘gender_2’]]
gender gender_2
0 男 1
1 男 1
2 女 0
3 男 1
4 女 0
5 女 0
不论是利用字典还是函数进行映射,map方法都是把对应的数据逐个当作参数传入到字典或函数中,得到映射后的值。
2、apply() 功能其实和map()很像
创建测试数据源
import numpy as np
import pandas as pd
boolean=[True,False]
gender=[“男”,“女”]
color=[“white”,“black”,“yellow”]
data=pd.DataFrame({
“height”:np.random.randint(150,190,6),
“weight”:np.random.randint(40,90,6),
“smoker”:[boolean[x] for x in np.random.randint(0,2,6)],
“gender”:[gender[x] for x in np.random.randint(0,2,6)],
“age”:np.random.randint(15,90,6),
“color”:[color[x] for x in np.random.randint(0,len(color),6) ]
})
data
height weight smoker gender age color
0 161 54 False 男 87 white
1 173 78 False 女 77 yellow
2 185 50 True 男 65 white
3 167 65 True 女 49 yellow
4 158 54 True 女 83 white
5 181 56 True 男 32 yellow
apply() 求和
data[[“height”,“weight”,“age”]].apply(np.sum,axis=0)
height 1007
weight 364
age 337
dtype: int64
apply() 取对数
data[[“height”,“weight”,“age”]].apply(np.log, axis=0)
height weight age
0 5.159055 4.382027 3.970292
1 5.187386 3.891820 4.219508
2 5.056246 4.158883 4.110874
3 5.204007 4.343805 4.290459
4 5.105945 3.737670 3.891820
5 5.010635 3.951244 3.496508
apply() 传入函数
def BMI(data):
weight = data[‘weight’]
height = data[‘height’]/100
BMI = weight/height**2
return BMI
data[‘BMI’] = data.apply(BMI, axis=1)
data
height weight smoker gender age color BMI
0 174 80 True 女 53 black 26.423570
1 179 49 True 男 68 black 15.292906
2 157 64 False 男 61 yellow 25.964542
3 182 77 False 女 73 white 23.245985
4 165 42 True 男 49 white 15.426997
5 150 52 True 女 33 yellow 23.111111
apply() 用匿名函数实现
data[‘bbb’] = data.apply(lambda x:x[‘weight’]/((x[‘height’]/100)**2),axis=1)
data
height weight smoker gender age color BMI bbb
0 174 80 True 女 53 black 26.423570 26.423570
1 179 49 True 男 68 black 15.292906 15.292906
2 157 64 False 男 61 yellow 25.964542 25.964542
3 182 77 False 女 73 white 23.245985 23.245985
4 165 42 True 男 49 white 15.426997 15.426997
5 150 52 True 女 33 yellow 23.111111 23.111111
3、applymap() 会对DataFrame中的每个单元格执行指定函数的操作
创建测试数据源
df = pd.DataFrame(
{
“A”:np.random.randn(5),
“B”:np.random.randn(5),
“C”:np.random.randn(5),
“D”:np.random.randn(5),
“E”:np.random.randn(5),
}
)
df
A B C D E
0 -2.187775 -1.745212 0.995669 -0.704973 -1.391891
1 -1.249293 2.636823 0.280112 -1.676235 0.038624
2 -0.278298 -0.470418 -1.615759 -1.785880 0.233765
3 -0.446566 0.596736 0.419343 -0.139570 0.329558
4 -0.346454 0.285782 -1.244479 0.026556 -0.792457
传入匿名函数
df.applymap(lambda x:"%.2f"%x)
df.applymap(lambda x:round(x,2))
A B C D E
0 -2.06 1.06 1.54 0.27 2.15
1 -0.62 0.40 0.44 2.16 0.32
2 0.80 -1.29 0.63 0.13 0.98
3 -0.38 0.96 -0.29 -1.17 0.45
4 -0.32 1.05 -0.41 -0.04 0.49