对pandas中Series的map函数详解


Posted in Python onJuly 25, 2018

Series的map方法可以接受一个函数或含有映射关系的字典型对象。

使用map是一种实现元素级转换以及其他数据清理工作的便捷方式。

(DataFrame中对应的是applymap()函数,当然DataFrame还有apply()函数)

1、字典映射

import pandas as pd
from pandas import Series, DataFrame

data = DataFrame({'food':['bacon','pulled pork','bacon','Pastrami',
   'corned beef','Bacon','pastrami','honey ham','nova lox'],
     'ounces':[4,3,12,6,7.5,8,3,5,6]})
meat_to_animal = {
 'bacon':'pig',
 'pulled pork':'pig',
 'pastrami':'cow',
 'corned beef':'cow',
 'honey ham':'pig',
 'nova lox':'salmon' } 

data['animal'] = data['food'].map(str.lower).map(meat_to_animal) 
data 

data['food'].map(lambda x: meat_to_animal[x.lower()])

2、应用函数

In [579]: import pandas as pd

In [580]: from pandas import Series, DataFrame

In [581]: index = pd.date_range('2017-08-15', periods=10)

In [582]: ser = Series(list(range(10)), index=index)

In [583]: ser
Out[583]: 
2017-08-15 0
2017-08-16 1
2017-08-17 2
2017-08-18 3
2017-08-19 4
2017-08-20 5
2017-08-21 6
2017-08-22 7
2017-08-23 8
2017-08-24 9
Freq: D, dtype: int64


In [585]: ser.index.map(lambda x: x.day)
Out[585]: Int64Index([15, 16, 17, 18, 19, 20, 21, 22, 23, 24], dtype='int64')

In [586]: ser.index.map(lambda x: x.weekday)
Out[586]: Int64Index([1, 2, 3, 4, 5, 6, 0, 1, 2, 3], dtype='int64')

In [587]: ser.map(lambda x: x+10)
Out[587]: 
2017-08-15 10
2017-08-16 11
2017-08-17 12
2017-08-18 13
2017-08-19 14
2017-08-20 15
2017-08-21 16
2017-08-22 17
2017-08-23 18
2017-08-24 19
Freq: D, dtype: int64

In [588]: def f(x):
  ...:  if x < 5:
  ...:   return True
  ...:  else:
  ...:   return False
  ...:  

In [589]: ser.map(f)
Out[589]: 
2017-08-15  True
2017-08-16  True
2017-08-17  True
2017-08-18  True
2017-08-19  True
2017-08-20 False
2017-08-21 False
2017-08-22 False
2017-08-23 False
2017-08-24 False
Freq: D, dtype: bool

以上这篇对pandas中Series的map函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python远程登录代码
Apr 29 Python
Python表示矩阵的方法分析
May 26 Python
Python 实现字符串中指定位置插入一个字符
May 02 Python
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
Feb 21 Python
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
Nov 28 Python
Django自带的加密算法及加密模块详解
Dec 03 Python
解决django migrate报错ORA-02000: missing ALWAYS keyword
Jul 02 Python
numpy中生成随机数的几种常用函数(小结)
Aug 18 Python
python安装cx_Oracle和wxPython的方法
Sep 14 Python
pytorch训练神经网络爆内存的解决方案
May 22 Python
用Python爬取各大高校并可视化帮弟弟选大学,弟弟直呼牛X
Jun 11 Python
Python+Pillow+Pytesseract实现验证码识别
May 11 Python
基于pandas将类别属性转化为数值属性的方法
Jul 25 #Python
Django实现支付宝付款和微信支付的示例代码
Jul 25 #Python
Python走楼梯问题解决方法示例
Jul 25 #Python
python 批量修改/替换数据的实例
Jul 25 #Python
django 实现电子支付功能的示例代码
Jul 25 #Python
python 去除txt文本中的空格、数字、特定字母等方法
Jul 24 #Python
Python将文本去空格并保存到txt文件中的实例
Jul 24 #Python
You might like
PHP中函数rand和mt_rand的区别比较
2012/12/26 PHP
php学习笔记之面向对象编程
2012/12/29 PHP
ThinkPHP3.1新特性之对Ajax的支持更加完善
2014/06/19 PHP
php实现银联商务公众号+服务窗支付的示例代码
2019/10/12 PHP
javascript操作cookie_获取与修改代码
2009/05/21 Javascript
JavaScript高级程序设计 阅读笔记(二十一) JavaScript中的XML
2012/09/14 Javascript
JS中window.open全屏命令解析及使用示例
2013/12/11 Javascript
js控制页面的全屏展示和退出全屏显示的方法
2015/03/10 Javascript
Bootstrap每天必学之前端开发框架
2015/11/19 Javascript
JavaScript 拖拽实例代码
2016/09/21 Javascript
NODE.JS跨域问题的完美解决方案
2016/10/20 Javascript
nodejs redis 发布订阅机制封装实现方法及实例代码
2016/12/15 NodeJs
移动端界面的适配
2017/01/11 Javascript
基于bootstrap实现收缩导航条
2017/03/17 Javascript
浅析bootstrap原理及优缺点
2017/03/19 Javascript
Angular实现图片裁剪工具ngImgCrop实践
2017/08/17 Javascript
vue图片上传组件使用详解
2019/12/23 Javascript
vue 使用async写数字动态加载效果案例
2020/07/18 Javascript
JS指定音频audio在某个时间点进行播放
2020/11/28 Javascript
JavaScript中跨域问题的深入理解
2021/03/04 Javascript
Python 内置函数memoryview(obj)的具体用法
2017/11/23 Python
python遍历文件夹找出文件夹后缀为py的文件方法
2018/10/21 Python
python分块读取大数据,避免内存不足的方法
2018/12/10 Python
pandas DataFrame创建方法的方式
2019/08/02 Python
导入tensorflow:ImportError: libcublas.so.9.0 报错
2020/01/06 Python
Python 模拟生成动态产生验证码图片的方法
2020/02/01 Python
基于python实现破解滑动验证码过程解析
2020/05/28 Python
html5实现canvas阴影效果示例
2014/05/07 HTML / CSS
应聘护士求职信
2014/07/21 职场文书
交通事故委托书范本
2014/09/28 职场文书
2014年领导班子工作总结
2014/12/11 职场文书
毕业生入职感言
2015/07/31 职场文书
2016年清明节网上祭英烈活动总结
2016/04/01 职场文书
如何理解PHP核心特性命名空间
2021/05/28 PHP
redis数据结构之压缩列表
2022/03/21 Redis
mysql如何查询连续记录
2022/05/11 MySQL