<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Pandas中的寶藏函數(shù)-map

          共 3323字,需瀏覽 7分鐘

           ·

          2021-07-28 02:52

          pandas中的map類似于Python內(nèi)建的map()方法,pandas中的map()方法將函數(shù)、字典索引或是一些需要接受單個輸入值的特別的對象與對應(yīng)的單個列的每一個元素建立聯(lián)系并串行得到結(jié)果。


          這里我們想要得到gender列的F、M轉(zhuǎn)換為女性、男性的新列,可以有以下幾種實現(xiàn)方式

          先構(gòu)造一個數(shù)據(jù)集


          map()函數(shù)可以用于Series對象或DataFrame對象的一列,接收函數(shù)作為或字典對象作為參數(shù),返回經(jīng)過函數(shù)或字典映射處理后的值。

           

          用法:Series.map(arg, na_action=None)

          參數(shù):

          arg : function, dict, or Series

            Mapping correspondence.

          na_action : {None, ‘ignore’}, default None

            If ‘ignore’, propagate NaN values, without passing them to the mapping

            correspondence.

          返回:Pandas Series with same as index as caller

          官方:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html

           


          首先構(gòu)建一個數(shù)據(jù)集,下面進(jìn)行案例應(yīng)用

          data = pd.DataFrame({"name":['Jack', 'Alice', 'Lily', 'Mshis', 'Gdli', 'Agosh', 'Filu', 'Mack', 'Lucy', 'Pony'],"gender":['F', 'M', 'F', 'F', 'M', 'F', 'M', 'M', 'F', 'F'],"age":[25, 34, 49, 42, 28, 23, 45, 21, 34, 29]}                     ) data name gender  age0   Jack      F   251  Alice      M   342   Lily      F   493  Mshis      F   424   Gdli      M   285  Agosh      F   236   Filu      M   457   Mack      M   218   Lucy      F   349   Pony      F   29

           

          1 字典映射

          這里我們編寫F、M與女性、男性之間一一映射的字典,再利用map()方法來得到映射列:

          #定義F->女性,M->男性的映射字典gender2xb = {'F': '女性', 'M': '男性'}
          #利用map()方法得到對應(yīng)gender列的映射列data.gender.map(gender2xb)0 女性1 男性2 女性3 女性4 男性5 女性6 男性7 男性8 女性9 女性

           

          2 lambda函數(shù)

          這里我們向map()中傳入lambda函數(shù)來實現(xiàn)所需功能:

          #因為已經(jīng)知道數(shù)據(jù)gender列性別中只有F和M所以編寫如下lambda函數(shù)
          data.gender.map(lambda x:'女性' if x == 'F' else '男性')0    女性1    男性2    女性3    女性4    男性5    女性6    男性7    男性8    女性9    女性
          #年齡的平方data.age.map(lambda x: x**2)0 6251 11562 24013 17644 7845 5296 20257 4418 11569 84

           

          常規(guī)函數(shù)

          map函數(shù),也可以傳入通過def定義的常規(guī)函數(shù),看看下面的案例

          #性別轉(zhuǎn)換def gender_to_xb(x):    return '女性' if x == 'F' else '男性'    data.gender.map(gender_to_xb)0    女性1    男性2    女性3    女性4    男性5    女性6    男性7    男性8    女性9    女性

           

          4 特殊對象

          map()可以傳入的內(nèi)容有時候可以很特殊,如下面的例子:一些接收單個輸入值且有輸出的對象也可以用map()方法來處理:

          data.gender.map("This kid's gender is {}".format)0    This kid's gender is F1    This kid's gender is M2    This kid's gender is F3    This kid's gender is F4    This kid's gender is M5    This kid's gender is F6    This kid's gender is M7    This kid's gender is M8    This kid's gender is F9    This kid's gender is F


          map()中的參數(shù)na_action,類似R中的na.action,取值為None或ingore,用于控制遇到缺失值的處理方式,設(shè)置為ingore時串行運(yùn)算過程中將忽略Nan值原樣返回。


          s = pd.Series(['cat', 'dog', np.nan, 'rabbit']) s0      cat1      dog2      NaN3   rabbit

          na_action為默認(rèn)值的情況

          s.map('I am a {}'.format)0       I am a cat1       I am a dog2       I am a nan3    I am a rabbit

          na_action為ignore的情況

          s.map('I am a {}'.format, na_action='ignore')0     I am a cat1     I am a dog2            NaN3  I am a rabbit

          ···  END  ···
          推薦閱讀:
          一、Number(數(shù)字)
          全面掌握Python基礎(chǔ),這一篇就夠了,建議收藏
          Python基礎(chǔ)之?dāng)?shù)字(Number)超級詳解
          Python隨機(jī)模塊22個函數(shù)詳解
          Python數(shù)學(xué)math模塊55個函數(shù)詳解
          二、String(字符串)
          Python字符串的45個方法詳解
          Pandas向量化字符串操作
          三、List(列表)
          超級詳解系列-Python列表全面解析
          Python輕量級循環(huán)-列表推導(dǎo)式
          四、Tuple(元組)
          Python的元組,沒想象的那么簡單
          五、Set(集合)
          全面理解Python集合,17個方法全解,看完就夠了
          六、Dictionary(字典)
          Python字典詳解-超級完整版
          七、內(nèi)置函數(shù)
          Python初學(xué)者必須吃透這69個內(nèi)置函數(shù)!
          八、正則模塊
          Python正則表達(dá)式入門到入魔
          筆記 | 史上最全的正則表達(dá)式
          八、系統(tǒng)操作
          Python之shutil模塊11個常用函數(shù)詳解
          Python之OS模塊39個常用函數(shù)詳解
          九、進(jìn)階模塊
          【萬字長文詳解】Python庫collections,讓你擊敗99%的Pythoner
          高手如何在Python中使用collections模塊

          掃描關(guān)注本號↓

          瀏覽 45
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  理论片人人操人人摸 | 日本的黄色视频 | 台湾一区二区三区在线 | 青青草91久久久久久久久 | 老熟妇乱伦视频 |