Pandas的数据转换函数map、apply、applymap
2023-09-11 14:17:06 时间
目录
数据转换函数对比:map、apply、applymap:
- map:只用于Series,实现每个值->值的映射;
- apply:用于Series实现每个值的处理,用于Dataframe实现某个轴的Series的处理;
- applymap:只能用于DataFrame,用于处理该DataFrame的每个元素;
1. map用于Series值的转换
实例:将股票代码英文转换成中文名字
Series.map(dict) or Series.map(function)均可
>>> import pandas as pd
>>> df = pd.read_csv('/lianxi/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
#################################
isp中文映射,注意这里是小写
>>> dict_isp_names = {
... "cmnet": "中国移动",
... "cmcc": "中国联通",
... "ctc": "中国电信"
... }
方法1:Series.map(dict)
>>> 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 中国联通
#################################
方法2:Series.map(function)
function的参数是Series的每个元素的值
>>> 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
>>> 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
#################################
2. apply用于Series和DataFrame的转换
- Series.apply(function), 函数的参数是每个值
- DataFrame.apply(function), 函数的参数是Series
Series.apply(function)
function的参数是Series的每个值
>>> 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 中国联通
>>>
#################################
DataFrame.apply(function)
function的参数是对应轴的Series
>>> df['total'] = df[['pv','uv']].apply(lambda x : x.sum(), axis = 1)
>>> df.head()
date prov isp pv uv isp1 isp2 float_column float_column_1 isp3 total
0 2020-04-26 hunan cmnet 2000 1000 中国移动 中国移动 5.67435 5.674 中国移动 3000
1 2020-04-26 hunan cmnet 3000 1500 中国移动 中国移动 5.67435 5.674 中国移动 4500
2 2020-04-26 hunan cmcc 4000 1000 中国联通 中国联通 5.67435 5.674 中国联通 5000
3 2020-04-26 hubei ctc 2500 1000 中国电信 中国电信 5.67435 5.674 中国电信 3500
4 2020-04-26 hubei cmcc 2000 1000 中国联通 中国联通 5.67435 5.674 中国联通 3000
注意这个代码:
1、apply是在df[['pv','uv']]这个DataFrame上调用;
2、lambda x的x是一个Series,axis=1表示跨列,axis=0 表示跨行
>>> df.loc['total'] = df[['pv','uv']].apply(lambda x : x.sum(), axis = 0)
>>> df.tail()
date prov isp pv uv isp1 isp2 float_column float_column_1 isp3 total
8 2020-04-27 hunan cmcc 2800.0 1600.0 中国联通 中国联通 5.67435 5.674 中国联通 4400.0
9 2020-04-27 hubei ctc 2600.0 1400.0 中国电信 中国电信 5.67435 5.674 中国电信 4000.0
10 2020-04-27 hubei cmcc 3800.0 1900.0 中国联通 中国联通 5.67435 5.674 中国联通 5700.0
11 2020-04-27 hubei ctc 2400.0 1900.0 中国电信 中国电信 5.67435 5.674 中国电信 4300.0
total NaN NaN NaN 34400.0 17100.0 NaN NaN NaN NaN NaN NaN
>>>
#################################
3. 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))
>>> sub_df.head()
pv uv
0 2000 1000
1 3000 1500
2 4000 1000
3 2500 1000
4 2000 1000
相关文章
- Word控件Spire.Doc 转换教程(十七): 加载纯文本 (txt) 文件并保存为 word 文档
- Flutter OpenContainer 容器转换过渡 Material Design 设计风格的实践
- Google Earth Engine——在 Python 中将纬度/经度坐标转换为 Landsat 8 路径/行
- 什么是包装类?为什么要有包装类?基本类型与包装类如何转换?
- 利用cvtColor实现图像颜色空间的转换-比如RGB转灰度图等
- OpenCV1.x中对图像数据进行64F和8U转换的方法
- 关于postgresql:将字符串转换为整数-Postgres
- Java 8 将Map转换为List
- lodash用法系列(4),使用Map/Reduce转换
- python中数字和字符串和bytes的相互转换实例解析
- 《点睛:ActionScript3.0游戏互动编程》——1.3 HSB模式及其与RGB间的转换
- 浮点数存储方式理解,浮点数和整数之间的转换
- (数据科学学习手札60)用Python实现WGS84、火星坐标系、百度坐标系、web墨卡托四种坐标相互转换
- JS中将一个值转换为字符串的3种方法
- std::stringstream转换为std::vector<char>
- Map中使用和遍历map方法
- poi excel自己主动转换成javabean 支持引用类型属性二级转换
- python经常使用的十进制、16进制、字符串、字节串之间的转换(长期更新帖)
- ArcMap 转换地图坐标系
- java set转list,数组与list的转换