对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计算auc指标实例
Jul 13 Python
在CentOS6上安装Python2.7的解决方法
Jan 09 Python
Python实现数据可视化看如何监控你的爬虫状态【推荐】
Aug 10 Python
Python requests模块实例用法
Feb 11 Python
啥是佩奇?使用Python自动绘画小猪佩奇的代码实例
Feb 20 Python
python3中rank函数的用法
Nov 27 Python
flask 实现上传图片并缩放作为头像的例子
Jan 09 Python
pycharm运行程序时看不到任何结果显示的解决
Feb 21 Python
Keras设定GPU使用内存大小方式(Tensorflow backend)
May 22 Python
Python网页解析器使用实例详解
May 30 Python
解决django migrate报错ORA-02000: missing ALWAYS keyword
Jul 02 Python
通过实例了解python__slots__使用方法
Sep 14 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
PHP4之COOKIE支持详解
2006/10/09 PHP
使用php清除bom示例
2014/03/03 PHP
一组PHP加密解密函数分享
2014/06/05 PHP
简单分析ucenter 会员同步登录通信原理
2014/08/25 PHP
php中获取主机名、协议及IP地址的方法
2014/11/18 PHP
PHP数组编码gbk与utf8互相转换的两种方法
2016/09/01 PHP
JavaScript之引用类型介绍
2012/08/10 Javascript
javascript ready和load事件的区别示例介绍
2013/08/30 Javascript
jQuery DOM删除节点操作指南
2015/03/03 Javascript
Jquery中request和request.form和request.querystring的区别
2015/11/26 Javascript
网页瀑布流布局jQuery实现代码
2016/10/21 Javascript
jQuery设置Easyui校验规则(推荐)
2016/11/21 Javascript
vue.js整合mint-ui里的轮播图实例代码
2017/12/27 Javascript
JS实现把一个页面层数据传递到另一个页面的两种方式
2018/08/13 Javascript
ES6中的class是如何实现的(附Babel编译的ES5代码详解)
2019/05/17 Javascript
ES6小技巧之代替lodash
2019/06/07 Javascript
在Express中提供静态文件的实现方法
2019/10/17 Javascript
python教程之用py2exe将PY文件转成EXE文件
2014/06/12 Python
YUV转为jpg图像的实现
2019/12/09 Python
django数据模型(Model)的字段类型解析
2019/12/25 Python
Python小白学习爬虫常用请求报头
2020/06/03 Python
Python绘制组合图的示例
2020/09/18 Python
年终总结会主持词
2014/03/25 职场文书
高中教师评语大全
2014/04/25 职场文书
中班上学期幼儿评语
2014/04/30 职场文书
精神文明建设先进工作者事迹材料
2014/05/02 职场文书
设计大赛策划方案
2014/06/13 职场文书
我的梦想演讲稿500字
2014/08/21 职场文书
党员干部四风问题整改措施思想汇报
2014/10/12 职场文书
2014年生产管理工作总结
2014/12/23 职场文书
大学生学年个人总结
2015/02/15 职场文书
党小组考察意见
2015/06/02 职场文书
毛主席纪念堂观后感
2015/06/17 职场文书
酒店厨房管理制度
2015/08/06 职场文书
青年干部培训班学习心得体会
2016/01/06 职场文书
Redis读写分离搭建的完整步骤
2021/09/14 Redis