對(duì)pandas中Series的map函數(shù)詳解
Series的map方法可以接受一個(gè)函數(shù)或含有映射關(guān)系的字典型對(duì)象。
使用map是一種實(shí)現(xiàn)元素級(jí)轉(zhuǎn)換以及其他數(shù)據(jù)清理工作的便捷方式。
(DataFrame中對(duì)應(yīng)的是applymap()函數(shù),當(dāng)然DataFrame還有apply()函數(shù))
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、應(yīng)用函數(shù)
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
以上這篇對(duì)pandas中Series的map函數(shù)詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)加密的RAR文件解壓的方法(密碼已知)
這篇文章主要介紹了Python實(shí)現(xiàn)加密的RAR文件解壓,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Django處理文件上傳File Uploads的實(shí)例
今天小編就為大家分享一篇Django處理文件上傳File Uploads的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
python使用time、datetime返回工作日列表實(shí)例代碼
這篇文章主要介紹了python使用time、datetime返回工作日列表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python操作JSON實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換
這篇文章主要介紹了Python操作JSON實(shí)現(xiàn)網(wǎng)絡(luò)數(shù)據(jù)交換,JSON的全稱是 JavaScript Object Notation,是一種輕量級(jí)的數(shù)據(jù)交換格式,關(guān)于JSON的更多相關(guān)內(nèi)容感興趣的小伙伴可以參考一下2022-06-06
Python深度學(xué)習(xí)實(shí)戰(zhàn)PyQt5安裝與環(huán)境配置過(guò)程詳解
本系列面向 Python 小白,從零開(kāi)始實(shí)戰(zhàn)解說(shuō)應(yīng)用 QtDesigner 進(jìn)行 PyQt5 的項(xiàng)目實(shí)戰(zhàn)。什么叫從零開(kāi)始?從軟件安裝、環(huán)境配置開(kāi)始。不跳過(guò)一個(gè)細(xì)節(jié),不漏掉一行代碼,不省略一個(gè)例圖2021-10-10
python爬蟲beautifulsoup庫(kù)使用操作教程全解(python爬蟲基礎(chǔ)入門)
這篇文章主要介紹了python爬蟲beautifulsoup庫(kù)使用操作全解(python爬蟲基礎(chǔ)入門),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
python網(wǎng)絡(luò)爬蟲精解之XPath的使用說(shuō)明
XPath 是一門在 XML 文檔中查找信息的語(yǔ)言。XPath 可用來(lái)在 XML 文檔中對(duì)元素和屬性進(jìn)行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 都構(gòu)建于 XPath 表達(dá)之上2021-09-09
python使用socket向客戶端發(fā)送數(shù)據(jù)的方法
這篇文章主要介紹了python使用socket向客戶端發(fā)送數(shù)據(jù)的方法,涉及Python使用socket實(shí)現(xiàn)數(shù)據(jù)通信的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

