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

