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

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時(shí)代數(shù)據(jù)分析師教程《Python數(shù)據(jù)分析極簡入門》第2節(jié) 9 Pandas 文本數(shù)據(jù)
數(shù)據(jù)分析師教程《Python數(shù)據(jù)分析極簡入門》第2節(jié) 9 Pandas 文本數(shù)據(jù)
2024-11-29
收藏

《Python數(shù)據(jù)分析極簡入門》

第3節(jié) 9 Pandas 文本數(shù)據(jù)

import pandas as pd

1、cat() 拼接字符串

d = pd.DataFrame(['a''b''c'],columns = ['A'])
d
A
0 a
1 b
2 c

將某列元素拼接一列特定字符串

d['A'].str.cat(['A''B''C'], sep=',')
0    a,A
1    b,B
2    c,C
Name: A, dtype: object

將某列的元素合并為一個(gè)字符串

d['A'].str.cat(sep=',')
'a,b,c'

2、split() 切分字符串

import numpy as np
import pandas as pd
d = pd.DataFrame(['a_b_c''c_d_e', np.nan, 'f_g_h'],columns = ['A'])
d
A
0 a_b_c
1 c_d_e
2 NaN
3 f_g_h

將某列的字符串元素進(jìn)行切分

d['A'].str.split('_')
0    [a, b, c]
1    [c, d, e]
2          NaN
3    [f, g, h]
Name: A, dtype: object

3、get() 獲取指定位置的字符串

d = pd.DataFrame(['a_b_c''c_d_e', np.nan, 'f_g_h'],columns = ['A'])
d['A']
0    a_b_c
1    c_d_e
2      NaN
3    f_g_h
Name: A, dtype: object
d['A'].str.get(2)
0      b
1      d
2    NaN
3      g
Name: A, dtype: object

4、join() 對每個(gè)字符都用給定的字符串拼接起來(不常用)

d = pd.DataFrame(['a_b_c''c_d_e', np.nan, 'f_g_h'],columns = ['A'])
d['A']
0    a_b_c
1    c_d_e
2      NaN
3    f_g_h
Name: A, dtype: object
d['A'].str.join("!")
0    a!_!b!_!c
1    c!_!d!_!e
2          NaN
3    f!_!g!_!h
Name: A, dtype: object

5、contains() 是否包含表達(dá)式 (很常用)

d['A'].str.contains('d')
0    False
1     True
2      NaN
3    False
Name: A, dtype: object
d.fillna('0')[d.fillna('0')['A'].str.contains('d')]
A
1 c_d_e
d.fillna('0')[d['A'].fillna('0').str.contains('d|e')]

#表示或的關(guān)系用"A|B",表示且用'A.*B|B.*A'
A
1 c_d_e

6、replace() 替換

d['A'].str.replace("_"".")
0    a.b.c
1    c.d.e
2      NaN
3    f.g.h
Name: A, dtype: object

7、repeat() 重復(fù)

d['A'].str.repeat(3)
0    a_b_ca_b_ca_b_c
1    c_d_ec_d_ec_d_e
2                NaN
3    f_g_hf_g_hf_g_h
Name: A, dtype: object

8、pad() 左右補(bǔ)齊

d['A'].str.pad(10, fillchar="0")
0    00000a_b_c
1    00000c_d_e
2           NaN
3    00000f_g_h
Name: A, dtype: object
d['A'].str.pad(10, side="right", fillchar="?")
0    a_b_c?????
1    c_d_e?????
2           NaN
3    f_g_h?????
Name: A, dtype: object

9、center() 中間補(bǔ)齊

d['A'].str.center(10, fillchar="?")
0    ??a_b_c???
1    ??c_d_e???
2           NaN
3    ??f_g_h???
Name: A, dtype: object

10、ljust() 右邊補(bǔ)齊

d['A'].str.ljust(10, fillchar="?")
0    a_b_c?????
1    c_d_e?????
2           NaN
3    f_g_h?????
Name: A, dtype: object

11、rjust() 左邊補(bǔ)齊

d['A'].str.rjust(10, fillchar="?")
0    ?????a_b_c
1    ?????c_d_e
2           NaN
3    ?????f_g_h
Name: A, dtype: object

12、zfill() 左邊補(bǔ)0

d['A'].str.zfill(10)
0    00000a_b_c
1    00000c_d_e
2           NaN
3    00000f_g_h
Name: A, dtype: object

13、wrap() 在指定的位置加回車符號

d['A'].str.wrap(3)
0    a_bn_c
1    c_dn_e
2        NaN
3    f_gn_h
Name: A, dtype: object

14、slice() 按給定點(diǎn)的開始結(jié)束位置切割字符串

d['A'].str.slice(1,3)
0     _b
1     _d
2    NaN
3     _g
Name: A, dtype: object

15、slice_replace() 使用給定的字符串,替換指定的位置的字符

d['A'].str.slice_replace(13"?")
0    a?_c
1    c?_e
2     NaN
3    f?_h
Name: A, dtype: object

16、count() 計(jì)算給定單詞出現(xiàn)的次數(shù)

d['A'].str.count("b")
0    1.0
1    0.0
2    NaN
3    0.0
Name: A, dtype: float64

17、startswith() 判斷是否以給定的字符串開頭

d['A'].str.startswith("a")
0     True
1    False
2      NaN
3    False
Name: A, dtype: object

18、endswith() 判斷是否以給定的字符串結(jié)束

d['A'].str.endswith("e")
0    False
1     True
2      NaN
3    False
Name: A, dtype: object

19、findall() 查找所有符合正則表達(dá)式的字符,以數(shù)組形式返回

d['A'].str.findall("[a-z]")
0    [a, b, c]
1    [c, d, e]
2          NaN
3    [f, g, h]
Name: A, dtype: object

20、match() 檢測是否全部匹配給點(diǎn)的字符串或者表達(dá)式

d['A'].str.match("[d-z]")
0    False
1    False
2      NaN
3     True
Name: A, dtype: object

21、extract() 抽取匹配的字符串出來,注意要加上括號,把你需要抽取的東西標(biāo)注上

d['A'].str.extract("([d-z])")
0
0 NaN
1 d
2 NaN
3 f

22、len() 計(jì)算字符串的長度

d['A'].str.len()
0    5.0
1    5.0
2    NaN
3    5.0
Name: A, dtype: float64

23、strip() 去除前后的空白字符

df = pd.DataFrame(['a_b  ''  d_e  ', np.nan, 'f_g  '],columns = ['B'])
df['B']
0      a_b  
1      d_e  
2        NaN
3      f_g  
Name: B, dtype: object
df['B'].str.strip()
0    a_b
1    d_e
2    NaN
3    f_g
Name: B, dtype: object

24、rstrip() 去除后面的空白字符

df['B'].str.rstrip()
0      a_b
1      d_e
2      NaN
3      f_g
Name: B, dtype: object

25、lstrip() 去除前面的空白字符

df['B'].str.lstrip()
0    a_b  
1    d_e  
2      NaN
3    f_g  
Name: B, dtype: object

26、partition() 把字符串?dāng)?shù)組切割稱為DataFrame,注意切割只是切割稱為三部分,分隔符前,分隔符,分隔符后

d['A'] .str.partition('_')
0 1 2
0 a _ b_c
1 c _ d_e
2 NaN NaN NaN
3 f _ g_h

27、rpartition() 從右切起

d['A'].str.rpartition('_')
0 1 2
0 a_b _ c
1 c_d _ e
2 NaN NaN NaN
3 f_g _ h

28、lower() 全部小寫

d['A'].str.lower() 
0    a_b_c
1    c_d_e
2      NaN
3    f_g_h
Name: A, dtype: object

29、upper() 全部大寫

d['A'].str.upper() 
0    A_B_C
1    C_D_E
2      NaN
3    F_G_H
Name: A, dtype: object

30、find() 從左邊開始,查找給定字符串的所在位置

d['A'].str.find('d')
0   -1.0
1    2.0
2    NaN
3   -1.0
Name: A, dtype: float64

31、rfind() 從右邊開始,查找給定字符串的所在位置

d['A'].str.rfind('d')
0   -1.0
1    2.0
2    NaN
3   -1.0
Name: A, dtype: float64

32、index() 查找給定字符串的位置,注意,如果不存在這個(gè)字符串,那么會報(bào)錯(cuò)!

d['A'].str.index('_')
0    1.0
1    1.0
2    NaN
3    1.0
Name: A, dtype: float64

33、rindex() 從右邊開始查找,給定字符串的位置

d['A'].str.rindex('_')
0    3.0
1    3.0
2    NaN
3    3.0
Name: A, dtype: float64

34、capitalize() 首字符大寫

d['A'].str.capitalize()
0    A_b_c
1    C_d_e
2      NaN
3    F_g_h
Name: A, dtype: object

35、swapcase() 大小寫互換

d['A'].str.capitalize()
0    A_b_c
1    C_d_e
2      NaN
3    F_g_h
Name: A, dtype: object

36、isalnum() 是否全部是數(shù)字和字母組成

d['A'].str.isalnum()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

37、isalpha() 是否全部是字母

d['A'].str.isalpha()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

38、isdigit() 是否全部都是數(shù)字

d['A'].str.isdigit()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

39、isspace() 是否空格

d['A'].str.isspace()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

40、islower() 是否全部小寫

d['A'].str.islower()
0    True
1    True
2     NaN
3    True
Name: A, dtype: object

41、isupper() 是否全部大寫

d['A'].str.isupper()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

42、istitle() 是否只有首字母為大寫,其他字母為小寫

d['A'].str.istitle()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

43、isnumeric() 是否是數(shù)字

d['A'].str.isnumeric()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

44、isdecimal() 是否全是數(shù)字

d['A'].str.isdecimal()
0    False
1    False
2      NaN
3    False
Name: A, dtype: object

數(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(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) 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){ //倒計(jì)時(shí)完成 $(".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); }