一、三种数据转换函数:map、apply、applymap
1.map:只用于Series,实现每个值->值的映射;
2.apply:既可以用于Series实现每个值的处理,也可以用于Dataframe实现某个轴的Series的处理;
3.applymap:只能用于DataFrame,用于处理该DataFrame的每个元素;
二、map用于Series值的转换
1.实例:将股票代码英文转换成中文名字
Series里面传入一个字典和函数都可以,即Series.map(dict) or Series.map(function)
import pandas as pd
df = pd. read_csv ( './datas/read_test.csv' )
df. head ( )
date prov isp pv uv
0 2020 - 04 - 26 hunan cmnet 2000 1000
1 2020 - 04 - 26 hunan cmnet 3000 1500
2 2020 - 04 - 26 hunan cmcc 4000 1000
3 2020 - 04 - 26 hubei ctc 2500 1000
4 2020 - 04 - 26 hubei cmcc 2000 1000
1.1 、方法一,用字典;Series. map ( dict)
# isp 中文映射,注意这里是小写
#设置字典和映射关系
dict_isp_names = {
"cmnet" : "中国移动" ,
"cmcc" : "中国联通" ,
"ctc" : "中国电信"
}
#使用map方法
#因为原来的DataFrame中没有isp1列,所以添加一个新的列,叫isp1,通过设置的字典和映射关系来添加
df[ "isp1" ] = df[ "isp" ] . map ( dict_isp_names)
df. head ( )
date prov isp pv uv isp1
0 2020 - 04 - 26 hunan cmnet 2000 1000 中国移动
1 2020 - 04 - 26 hunan cmnet 3000 1500 中国移动
2 2020 - 04 - 26 hunan cmcc 4000 1000 中国联通
3 2020 - 04 - 26 hubei ctc 2500 1000 中国电信
4 2020 - 04 - 26 hubei cmcc 2000 1000 中国联通
1.2 、方法2 :用函数;Series. map ( function)
# function 的参数是Series的每个元素的值
#使用map方法
#因为原来的DataFrame中没有isp2列,所以添加一个新的列,叫isp2,通过函数和字典的取值来添加
df[ "isp2" ] = df[ "isp" ] . map ( lambda x : dict_isp_names[ x] )
df. head ( )
date prov isp pv uv isp1 isp2
0 2020 - 04 - 26 hunan cmnet 2000 1000 中国移动 中国移动
1 2020 - 04 - 26 hunan cmnet 3000 1500 中国移动 中国移动
2 2020 - 04 - 26 hunan cmcc 4000 1000 中国联通 中国联通
3 2020 - 04 - 26 hubei ctc 2500 1000 中国电信 中国电信
4 2020 - 04 - 26 hubei cmcc 2000 1000 中国联通 中国联通
df[ 'float_column' ] = 5.67435
df. head ( )
date prov isp pv uv isp1 isp2 float_column
0 2020 - 04 - 26 hunan cmnet 2000 1000 中国移动 中国移动 5.67435
1 2020 - 04 - 26 hunan cmnet 3000 1500 中国移动 中国移动 5.67435
2 2020 - 04 - 26 hunan cmcc 4000 1000 中国联通 中国联通 5.67435
3 2020 - 04 - 26 hubei ctc 2500 1000 中国电信 中国电信 5.67435
4 2020 - 04 - 26 hubei cmcc 2000 1000 中国联通 中国联通 5.67435
#使用map方法
#对df[ 'float_column' ] 进行格式化,保留小数点后三位,并添加到新的列float_column_1中
df[ 'float_column_1' ] = df[ 'float_column' ] . map ( lambda x: '%.3f' % x)
df. head ( )
date prov isp pv uv isp1 isp2 float_column float_column_1
0 2020 - 04 - 26 hunan cmnet 2000 1000 中国移动 中国移动 5.67435 5.674
1 2020 - 04 - 26 hunan cmnet 3000 1500 中国移动 中国移动 5.67435 5.674
2 2020 - 04 - 26 hunan cmcc 4000 1000 中国联通 中国联通 5.67435 5.674
3 2020 - 04 - 26 hubei ctc 2500 1000 中国电信 中国电信 5.67435 5.674
4 2020 - 04 - 26 hubei cmcc 2000 1000 中国联通 中国联通 5.67435 5.674
三、apply用于Series和DataFrame的转换
Series.apply(function), 函数的参数是每个值
DataFrame.apply(function), 函数的参数是Series
1、Series.apply(function)
function的参数是Series的每个值
#使用apply方法
#因为原来的DataFrame中没有isp3列,所以添加一个新的列,叫isp3,用函数和字典取值来添加数据
df[ "isp3" ] = df[ "isp" ] . apply (
lambda x : dict_isp_names[ x] )
df. head ( )
date prov isp pv uv isp1 isp2 float_column float_column_1 isp3
0 2020 - 04 - 26 hunan cmnet 2000 1000 中国移动 中国移动 5.67435 5.674 中国移动
1 2020 - 04 - 26 hunan cmnet 3000 1500 中国移动 中国移动 5.67435 5.674 中国移动
2 2020 - 04 - 26 hunan cmcc 4000 1000 中国联通 中国联通 5.67435 5.674 中国联通
3 2020 - 04 - 26 hubei ctc 2500 1000 中国电信 中国电信 5.67435 5.674 中国电信
4 2020 - 04 - 26 hubei cmcc 2000 1000 中国联通 中国联通 5.67435 5.674 中国联通
2、DataFrame.apply(function)
function的参数是对应轴的Series
#使用apply方法,对'pv' , 'uv' 这两列进行求和,得到的新的数据添加到新的列'total' 中
# axis = 1 ,代表列相加
# axis = 0 ,代表行相加
df[ 'total' ] = df[ [ 'pv' , 'uv' ] ] . apply ( lambda x : x. sum ( ) , axis = 1 )
df. head ( )
date prov isp pv uv isp1 isp2 total
0 2020 - 04 - 26 hunan cmnet 2000 1000 中国移动 中国移动 3000
1 2020 - 04 - 26 hunan cmnet 3000 1500 中国移动 中国移动 4500
2 2020 - 04 - 26 hunan cmcc 4000 1000 中国联通 中国联通 5000
3 2020 - 04 - 26 hubei ctc 2500 1000 中国电信 中国电信 3500
4 2020 - 04 - 26 hubei cmcc 2000 1000 中国联通 中国联通 3000
注意这个代码:
1 、apply是在df[ [ 'pv' , 'uv' ] ] 这个DataFrame上调用;
2 、lambda x的x是一个Series,axis= 1 表示跨列,axis= 0 表示跨行
#使用apply方法,对'pv' , 'uv' 这两列进行求和,得到的新的数据添加到新的行'total' 中
df. loc[ 'total' ] = df[ [ 'pv' , 'uv' ] ] . apply ( lambda x : x. sum ( ) , axis = 0 )
df. tail ( )
date prov isp pv uv isp1 isp2 total
8 2020 - 04 - 27 hunan cmcc 2800.0 1600.0 中国联通 中国联通 4400.0
9 2020 - 04 - 27 hubei ctc 2600.0 1400.0 中国电信 中国电信 4000.0
10 2020 - 04 - 27 hubei cmcc 3800.0 1900.0 中国联通 中国联通 5700.0
11 2020 - 04 - 27 hubei ctc 2400.0 1900.0 中国电信 中国电信 4300.0
total NaN NaN NaN 34400.0 17100.0 NaN NaN NaN
四、applymap用于DataFrame所有值的转换
sub_df = df[ [ 'pv' , 'uv' ] ]
sub_df. head ( )
pv uv
0 2000.0 1000.0
1 3000.0 1500.0
2 4000.0 1000.0
3 2500.0 1000.0
4 2000.0 1000.0
# 将这些数字取整数,应用于所有元素
sub_df = sub_df. applymap ( lambda x : int ( x) )
pv uv
0 2000 1000
1 3000 1500
2 4000 1000
3 2500 1000
4 2000 1000