99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Pandas中的寶藏函數(shù)-map
Pandas中的寶藏函數(shù)-map
2021-07-28
收藏

來源:AI入門學習

作者:小伍哥

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ù)集,下面進行案例應(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 age 0 Jack F 25 1 Alice M 34 2 Lily F 49 3 Mshis F 42 4 Gdli M 28 5 Agosh F 23 6 Filu M 45 7 Mack M 21 8 Lucy F 34 9 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 625 1 1156 2 2401 3 1764 4 784 5 529 6 2025 7 441 8 1156 9 84

3 常規(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 F 1 This kid's gender is M 2 This kid's gender is F 3 This kid's gender is F 4 This kid's gender is M 5 This kid's gender is F 6 This kid's gender is M 7 This kid's gender is M 8 This kid's gender is F 9 This kid's gender is F

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

s = pd.Series(['cat', 'dog', np.nan, 'rabbit']) s 0 cat 1 dog 2 NaN 3 rabbit

na_action為默認值的情況

s.map('I am a {}'.format) 0 I am a cat 1 I am a dog 2 I am a nan 3 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

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

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }