Pandas中map(),applymap(),apply()函數(shù)的使用方法
將函數(shù)應(yīng)用于pandas對(duì)象(pandas.DataFrame,pandas.Series)時(shí),根據(jù)所應(yīng)用的函數(shù)類(lèi)型以及是否將其應(yīng)用于元素,行或列,使用的方法會(huì)有所不同。
指定pandas對(duì)象作為NumPy函數(shù)的參數(shù)
- 將Pandas對(duì)象指定為函數(shù)參數(shù)
- 是否將其應(yīng)用于元素,行或列取決于函數(shù)的類(lèi)型和參數(shù)的設(shè)置而有所不同
- Pandas對(duì)象中的一些方法
Pandas對(duì)象方法的函數(shù)應(yīng)用
元素功能(標(biāo)量值)
- 適用于Series的每個(gè)元素:map(),apply()
- 應(yīng)用于DataFrame的每個(gè)元素:applymap()
行和列的功能(一維數(shù)組)
- 應(yīng)用于DataFrame的每行和每列:apply()
用以下csv文件作為示例進(jìn)行說(shuō)明。
a,b,c,d
11,12,13,14
21,22,23,24
31,32,33,34
import pandas as pd import numpy as np df = pd.read_csv('./data/06/sample_header.csv') print(df) # ? ? a ? b ? c ? d # 0 ?11 ?12 ?13 ?14 # 1 ?21 ?22 ?23 ?24 # 2 ?31 ?32 ?33 ?34
指定pandas對(duì)象作為NumPy函數(shù)的參數(shù)
可以將Pandas對(duì)象指定為NumPy函數(shù)的參數(shù)。
元素的應(yīng)用
NumPy的通用函數(shù)(ufunc:應(yīng)用于數(shù)組元素的函數(shù))適用于pandas對(duì)象的每個(gè)元素。
絕對(duì)值(fabs()),平方根(sqrt()),log(log())等。
print(np.sqrt(df)) # a b c d # 0 3.316625 3.464102 3.605551 3.741657 # 1 4.582576 4.690416 4.795832 4.898979 # 2 5.567764 5.656854 5.744563 5.830952
行/列的應(yīng)用
如果將pandas對(duì)象指定為從NumPy數(shù)組的所有元素計(jì)算值的函數(shù)的參數(shù),則默認(rèn)情況下它將應(yīng)用于pandas對(duì)象的每列。如果參數(shù)軸= 1,則將其應(yīng)用于每行。
最大值(amax()),最小值(amin()),平均值(mean())等。
print(np.amax(df)) # a ? ?31 # b ? ?32 # c ? ?33 # d ? ?34 # dtype: int64 print(np.mean(df, axis=1)) # 0 ? ?12.5 # 1 ? ?22.5 # 2 ? ?32.5 # dtype: float64
pandas.DataFrame,pandas.Series方法
最大值,最小值,平均值,方差等也被準(zhǔn)備為Pandas對(duì)象的方法,因此也可以直接使用它們。
同樣,在這種情況下,默認(rèn)情況下也會(huì)將其應(yīng)用于每一列,并且如果參數(shù)axis = 1,則會(huì)將其應(yīng)用于每一行。
print(df.max()) # a ? ?31 # b ? ?32 # c ? ?33 # d ? ?34 # dtype: int64 print(df.max(axis=1)) # 0 ? ?14 # 1 ? ?24 # 2 ? ?34 # dtype: int64
Pandas對(duì)象方法的函數(shù)應(yīng)用
可以使用pandas對(duì)象方法將函數(shù)應(yīng)用于元素,行和列。您可以應(yīng)用Python內(nèi)置函數(shù)或您定義的函數(shù)。
- 應(yīng)用于Series的每個(gè)元素:map(),apply()
- 應(yīng)用于DataFrame的每個(gè)元素:applymap()
- 應(yīng)用于DataFrame的每行和每列:apply()
- 應(yīng)用于DataFrame的特定行/列元素
以上方法都返回一個(gè)新的已處理的對(duì)象,而原始對(duì)象則保持不變。沒(méi)有像dropna()或fillna()那樣的參數(shù),因此,如果想更改原始對(duì)象本身時(shí),
df = df.applymap(function)
如上,用原始對(duì)象替換新對(duì)象并覆蓋它。
適用于Series的每個(gè)元素:map(),apply()
將Python內(nèi)置函數(shù),匿名函數(shù)(lambda)或def定義的函數(shù)傳遞給map()或apply()的參數(shù)。
s = df['a'] print(s) # 0 ? ?11 # 1 ? ?21 # 2 ? ?31 # Name: a, dtype: int64 f_brackets = lambda x: '[{}]'.format(x) print(s.map(f_brackets)) # 0 ? ?[11] # 1 ? ?[21] # 2 ? ?[31] # Name: a, dtype: object def f_str(x): ? ? return str(x).replace('1', 'One').replace('2', 'Two').replace('3', 'Three').replace('4', 'Four') print(s.map(f_str)) # 0 ? ? ?OneOne # 1 ? ? ?TwoOne # 2 ? ?ThreeOne # Name: a, dtype: object
對(duì)于map(),如果將字典dict指定為參數(shù),它將替換為元素。
應(yīng)用于DataFrame的每個(gè)元素:applymap()
將Python的內(nèi)置函數(shù),匿名函數(shù)(lambda)或def定義的函數(shù)傳遞為applymap()的參數(shù)。
f_oddeven = lambda x: 'odd' if x % 2 == 1 else 'even' print(df.applymap(f_oddeven)) # a b c d # 0 odd even odd even # 1 odd even odd even # 2 odd even odd even
應(yīng)用于DataFrame的每行和每列:apply()
將適用于一維數(shù)組的函數(shù)傳遞給apply()的參數(shù)。默認(rèn)情況下,它應(yīng)用于每列,如果axis = 1,則應(yīng)用于每行。
f_maxmin = lambda x: max(x) - min(x) print(df.apply(f_maxmin)) # a ? ?20 # b ? ?20 # c ? ?20 # d ? ?20 # dtype: int64 print(df.apply(f_maxmin, axis=1)) # 0 ? ?3 # 1 ? ?3 # 2 ? ?3 # dtype: int64
應(yīng)用于DataFrame的特定行/列元素
由于沒(méi)有方法僅將功能應(yīng)用于DataFrame的特定行/列元素,可執(zhí)行以下方法。
- 選擇行/列并應(yīng)用帶有map()或apply()的功能
- 覆蓋原始行/列
df['b'] = df['b'].map(f_str) print(df) # ? ? a ? ? ? ? b ? c ? d # 0 ?11 ? ?OneTwo ?13 ?14 # 1 ?21 ? ?TwoTwo ?23 ?24 # 2 ?31 ?ThreeTwo ?33 ?34 df.iloc[2] = df.iloc[2].map(f_str) print(df) # ? ? ? ? ? a ? ? ? ? b ? ? ? ? ? c ? ? ? ? ?d # 0 ? ? ? ?11 ? ?OneTwo ? ? ? ? ?13 ? ? ? ? 14 # 1 ? ? ? ?21 ? ?TwoTwo ? ? ? ? ?23 ? ? ? ? 24 # 2 ?ThreeOne ?ThreeTwo ?ThreeThree ?ThreeFour
到此這篇關(guān)于Pandas中map(),applymap(),apply()函數(shù)的使用方法的文章就介紹到這了,更多相關(guān)Pandas map(),applymap(),apply()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對(duì)python字典過(guò)濾條件的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python字典過(guò)濾條件的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01在PyTorch中自定義fit()函數(shù)中的操作代碼
當(dāng)在進(jìn)行有監(jiān)督學(xué)習(xí)時(shí),我們可以使用fit()函數(shù)對(duì)模型進(jìn)行訓(xùn)練,通過(guò)迭代優(yōu)化模型的參數(shù),使其能夠更好地?cái)M合訓(xùn)練數(shù)據(jù),本文給大家介紹了在PyTorch中自定義fit()函數(shù)中的操作代碼,感興趣的同學(xué)可以跟著小編一起來(lái)看看2024-05-05解決pycharm 工具欄Tool中找不到Run manager.py Task的問(wèn)題
今天小編就為大家分享一篇解決pycharm 工具欄Tool中找不到Run manager.py Task的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07詳解Python中使用base64模塊來(lái)處理base64編碼的方法
8bit的bytecode經(jīng)常會(huì)被用base64編碼格式保存,Python中自帶base64模塊對(duì)base64提供支持,這里我們就來(lái)詳解Python中使用base64模塊來(lái)處理base64編碼的方法,需要的朋友可以參考下2016-07-07解決pyinstaller打包運(yùn)行程序時(shí)出現(xiàn)缺少plotly庫(kù)問(wèn)題
這篇文章主要介紹了解決pyinstaller打包運(yùn)行程序時(shí)出現(xiàn)缺少plotly庫(kù)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06單步調(diào)試 step into/step out/step over 區(qū)
這篇文章主要介紹了單步調(diào)試 step into/step out/step over 區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11使用python3調(diào)用wxpy模塊監(jiān)控linux日志并定時(shí)發(fā)送消息給群組或好友
這篇文章主要介紹了使用python3調(diào)用wxpy模塊,監(jiān)控linux日志并定時(shí)發(fā)送消息給群組或好友,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06