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函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持萬仟网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

pythonpandas函数详解_对pandas中Series的map函数详解相关推荐

  1. pandas中Series的apply函数

    闲来无事 浏览源码 发现了一个特别有意思的函数 Series中的apply()函数 和大多数apply函数一样,这个函数传入的参数也是一个函数,目的是对传入的series的值进行批量的处理,不用我们手 ...

  2. Pandas中Series结构的切片详解以及常用技巧

    Series的切片:(1)Series使用标签切片运算与普通的Python切片运算不同:Series使用标签切片时,其末端是包含的 (2)Series使用Python切片运算即使用位置数值切片,其末端 ...

  3. python中使用函数的优点_在python中使用自定义初始化函数而不是`__init__`的好处...

    野外的一些API(例如setuptools内部)有类似的东西,他们使用它们的优势. __init__调用可以用于低级内部API,而公共构造函数被定义为类方法,用于构造对象的不同方式.例如,在 pkg_ ...

  4. python调用函数怎么错_在Python中从类调用函数时参数数目错误

    我试图用python编写一个遗传算法的实现.上面写着我用两个参数来调用它,而只有一个是允许的,但我肯定我不允许.在 以下是相关代码:class GA: def __init__(self, best, ...

  5. Python之pandas:pandas中to_csv()、read_csv()函数的index、index_col(不将索引列写入)参数详解之详细攻略

    Python之pandas:pandas中to_csv().read_csv()函数的index.index_col(不将索引列写入)参数详解之详细攻略 目录 pandas中to_csv().read ...

  6. Pandas中at、iat函数详解

    at 函数:通过行名和列名来取值(取行名为a, 列名为A的值) iat 函数:通过行号和列号来取值(取第1行,第1列的值) 本文给出at.iat常见的用法,并附上详细代码. 1. 首先创建一个Data ...

  7. pandas中loc和iloc函数的用法详解

    无论是loc还是iloc都是pandas中数据筛选的函数. 我们先聊一下loc函数,loc的全程是location,什么东西可以作为location?我们第一时间可能会想到标签. 在pandas读取文 ...

  8. pandas中dropna()参数详解

    pandas中dropna()参数详解 DataFrame.dropna( axis=0, how='any', thresh=None, subset=None, inplace=False) 1. ...

  9. pandas中使用rolling.corr函数计算两个时间序列数据列之间的滚动相关性(Rolling correlations)、例如,计算两种商品销售额之间的3个月的滚动相关性

    pandas中使用rolling.corr函数计算两个时间序列数据列之间的滚动相关性(Rolling correlations).例如,计算两种商品销售额之间的3个月的滚动相关性 目录

最新文章

  1. 又一次 Java 内存泄漏排查,新技能+1
  2. 算法------------数组----------------两个数组的交集 II
  3. 就是一个人写代码做软件项目也建议用版本管理器也要考虑采用异地容灾手段...
  4. bios x86保护模式下的软盘操作floppy
  5. 演练:在 Windows 窗体中承载 Windows Presentation Foundation 复合控件 【转载】
  6. Android高级开发专题晋升班
  7. 7-4 哈利·波特的考试 (25 分)(C语言实现)
  8. 什么是Nacos?Nacos注册配置中心介绍
  9. 实验楼 linux内核原理与分析,《Linux内核原理与分析》第一周作业 20189210
  10. Mysql Packet for query is too large解决方法
  11. python学了有什么用-python学来有什么用
  12. javascript -- 事件--事件流-- 冒泡 --捕获
  13. 博弈论:寻找先手必胜策略——Grundy值
  14. [Verilog]4 选 1 数据选择器
  15. MySQL+Navicat安装配置
  16. TMOD TCON SCON
  17. LInux 的流量限制
  18. CyanogenMod源码下载和编译 CM7
  19. 低调,中国的FPGA到底有多强?!
  20. FAT12文件系统 理解

热门文章

  1. IoT:template
  2. LeetCode之Sort List
  3. javascript 获取光标所选中的内容并插入到另一个文本框中(兼容ie和ff)
  4. 【转】使用Apache CXF开发WebServices服务端
  5. (转)测测你是否有搜索引擎依赖症
  6. 什么是回调函数?回调函数有什么缺点?如何解决回调地狱问题?
  7. 解决大众点评换设备无法用卷(你本次购买不符合活动规则)的问题
  8. Failed to read schema document ‘http://code.alibabatech.com/schema/dubbo/dubbo.xsd‘问题解决方法
  9. PHP开发中常见的安全问题详解和解决方法
  10. idae中spring mvc解决问题application context not configured for this file于spring框架使用中的原因