乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Pandas三大利器-map、apply、applymap

       天上飛雞 2020-09-25


      作者:Peter

      出品:尤而小屋

      Pandas三大利器-

      map、apply、applymap

      實(shí)際工作中,我們在利用 pandas進(jìn)行數(shù)據(jù)處理的時候,經(jīng)常會對數(shù)據(jù)框中的單行、多行(列也適用)甚至是整個數(shù)據(jù)進(jìn)行某種相同方式的處理,比如將數(shù)據(jù)中的 sex字段將 男替換成1,女替換成0。

      在這個時候,很容易想到的是 for循環(huán)。用 for循環(huán)是一種很簡單、直接的方式,但是運(yùn)行效率很低。本文中介紹了 pandas中的三大利器: map、apply、applymap 來解決上述同樣的需求。

      • map

      • apply

      • applymap

      —  01 

      模擬數(shù)據(jù)

      通過一個模擬的數(shù)據(jù)來說明3個函數(shù)的使用,在這個例子中學(xué)會了如何生成各種模擬數(shù)據(jù)。數(shù)據(jù)如下:

      1. import pandas as pd

      2. import numpy as np

      3. boolean = [True, False]

      4. gender = ['男','女']

      5. color = ['white','black','red']

      6. # 好好學(xué)習(xí)如何生成模擬數(shù)據(jù):非常棒的例子

      7. # 學(xué)會使用random模塊中的randint方法

      8. df = pd.DataFrame({'height':np.random.randint(160,190,100),

      9. 'weight':np.random.randint(60,90,100),

      10. 'smoker':[boolean[x] for x in np.random.randint(0,2,100)],

      11. 'gender':[gender[x] for x in np.random.randint(0,2,100)],

      12. 'age':np.random.randint(20,60,100),

      13. 'color':[color[x] for x in np.random.randint(0,len(color),100)]

      14. })

      15. df.head()

      —  02 

      map

      demo

      map() 會根據(jù)提供的函數(shù)對指定序列做映射。

      第一個參數(shù) function 以參數(shù)序列中的每一個元素調(diào)用 function 函數(shù),返回包含每次 function 函數(shù)返回值的新列表。

      1. map(function, iterable)

      實(shí)際數(shù)據(jù)

      將gender中男變成1,女變成0

      1. # 方式1:通過字典映射實(shí)現(xiàn)

      2. dic = {'男':1, '女':0} # 通過字典映射

      3. df1 = df.copy() # 副本,不破壞原來的數(shù)據(jù)df

      4. df1['gender'] = df1['gender'].map(dic)

      5. df1

      6. # 方式2:通過函數(shù)實(shí)現(xiàn)

      7. def map_gender(x):

      8. gender = 1 if x == '男' else 0

      9. return gender

      10. df2 = df.copy()

      11. # 將df['gender']這個S型數(shù)據(jù)中的每個數(shù)值傳進(jìn)去

      12. df2['gender'] = df2['gender'].map(map_gender)

      13. df2

      —  03 

      apply

      apply方法的作用原理和 map方法類似,區(qū)別在于 apply能夠傳入功能更為復(fù)雜的函數(shù),可以說 apply是 map的高級版

      pandas 的 apply() 函數(shù)可以作用于 Series 或者整個 DataFrame,功能也是自動遍歷整個 Series 或者 DataFrame, 對每一個元素運(yùn)行指定的函數(shù)。

      在 DataFrame對象的大多數(shù)方法中,都會有 axis這個參數(shù),它控制了你指定的操作是沿著0軸還是1軸進(jìn)行。 axis=0代表操作對 columns進(jìn)行, axis=1代表操作對 row進(jìn)行

      demo

      1. 上面的數(shù)據(jù)中將age字段的值都減去3,即加上-3

      1. def apply_age(x,bias):

      2. return x + bias

      3. df4 = df.copy()

      4. # df4['age']當(dāng)做第一個值傳給apply_age函數(shù),args是第二個參數(shù)

      5. df4['age'] = df4['age'].apply(apply_age,args=(-3,))

      1. 計算BMI指數(shù)

      1. # 實(shí)現(xiàn)計算BMI指數(shù):體重/身高的平方(kg/m^2)

      2. def BMI(x):

      3. weight = x['weight']

      4. height = x['height'] / 100

      5. BMI = weight / (height **2)

      6. return BMI

      7. df5 = df.copy()

      8. df5['BMI'] = df5.apply(BMI,axis=1) # df5現(xiàn)在就相當(dāng)于BMI函數(shù)中的參數(shù)x;axis=1表示在列上操作

      9. df5

      DataFrame型數(shù)據(jù)的 apply操作總結(jié):

      1. 當(dāng) axis=0時,對 每列columns執(zhí)行指定函數(shù);當(dāng) axis=1時,對 每行row執(zhí)行指定函數(shù)。

      2. 無論 axis=0還是 axis=1,其傳入指定函數(shù)的默認(rèn)形式均為 Series,可以通過設(shè)置 raw=True傳入 numpy數(shù)組。

      3. 對每個Series執(zhí)行結(jié)果后,會將結(jié)果整合在一起返回(若想有返回值,定義函數(shù)時需要 return相應(yīng)的值)

      apply實(shí)現(xiàn)需求

      通過apply方法實(shí)現(xiàn)上面的性別轉(zhuǎn)換需求。apply方法中傳進(jìn)來的第一個參數(shù)一定是函數(shù)

      —  04 

      applymap

      DF數(shù)據(jù)加1

      applymap函數(shù)用于對DF型數(shù)據(jù)中的每個元素執(zhí)行相同的函數(shù)操作,比如下面的加1:

      保留2位有效數(shù)字


      ---------End---------

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多