对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 json encode datetime类型
Dec 28 Python
浅析Python中signal包的使用
Nov 13 Python
解决Python出现_warn_unsafe_extraction问题的方法
Mar 24 Python
Python简单的制作图片验证码实例
May 31 Python
用Eclipse写python程序
Feb 10 Python
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
Feb 26 Python
Python 微信之获取好友昵称并制作wordcloud的实例
Feb 21 Python
python中@property和property函数常见使用方法示例
Oct 21 Python
Python实现不规则图形填充的思路
Feb 02 Python
Numpy一维线性插值函数的用法
Apr 22 Python
python 等差数列末项计算方式
May 03 Python
教你一分钟在win10终端成功安装Pytorch的方法步骤
Jan 28 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
先进的自动咖啡技术,真的可以取代咖啡师吗?
2021/03/06 冲泡冲煮
第六节--访问属性和方法
2006/11/16 PHP
php解压文件代码实现php在线解压
2014/02/13 PHP
CodeIgniter辅助之第三方类库third_party用法分析
2016/01/20 PHP
Nginx下ThinkPHP5的配置方法详解
2017/08/01 PHP
PHP hex2bin()函数用法讲解
2019/02/25 PHP
PHP使用phpunit进行单元测试示例
2019/09/23 PHP
php设计模式之模板模式实例分析【星际争霸游戏案例】
2020/03/24 PHP
javascript iframe编程相关代码
2009/12/28 Javascript
IE6图片加载的一个BUG解决方法
2010/07/13 Javascript
基于jquery的使ListNav兼容中文首字拼音排序的实现代码
2011/07/10 Javascript
ExtJS中文乱码之GBK格式编码解决方案及代码
2013/01/20 Javascript
showModalDialog在谷歌浏览器下会返回Null的解决方法
2013/11/27 Javascript
js获取 type=radio 值的方法
2014/05/09 Javascript
JavaScript定义类和对象的方法
2014/11/26 Javascript
跟我学习JScript的Bug与内存管理
2015/11/18 Javascript
Bootstrap页面布局基础知识全面解析
2016/06/13 Javascript
JS实现的多张图片轮流播放幻灯片效果
2016/07/22 Javascript
JS动态的把左边列表添加到右边的实现代码(可上下移动)
2016/11/17 Javascript
简单实现bootstrap导航效果
2017/02/07 Javascript
利用JS做网页特效_大图轮播(实例讲解)
2017/08/09 Javascript
利用Vue实现移动端图片轮播组件的方法实例
2017/08/23 Javascript
node 文件上传接口的转发的实现
2019/09/23 Javascript
VUE:vuex 用户登录信息的数据写入与获取方式
2019/11/11 Javascript
Python3实现简单可学习的手写体识别(实例讲解)
2017/10/21 Python
用python的turtle模块实现给女票画个小心心
2019/11/23 Python
tensorflow 实现从checkpoint中获取graph信息
2020/02/10 Python
Set里的元素是不能重复的,那么用什么方法来区分重复与否呢?
2016/08/18 面试题
关于母亲节的感言
2014/02/04 职场文书
演讲主持词
2014/03/18 职场文书
我们的节日元宵活动方案
2014/08/23 职场文书
医生爱岗敬业演讲稿
2014/08/26 职场文书
民事和解协议书格式
2014/11/29 职场文书
匿名检举信范文
2015/03/02 职场文书
Golang使用Panic与Recover进行错误捕获
2022/03/22 Golang
使用CSS实现百叶窗效果示例代码
2023/05/07 HTML / CSS