国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
數(shù)據(jù)分析實(shí)戰(zhàn)
        在使用pandas框架的DataFrame的過程中,如果需要處理一些字符串的特性,例如判斷某列是否包含一些關(guān)鍵字,某列的字符長(zhǎng)度是否小于3等等這種需求,如果掌握str列內(nèi)置的方法,處理起來會(huì)方便很多。

        下面我們來詳細(xì)了解一下,Series類的str自帶的方法有哪些。

1、cat() 拼接字符串
        例子:
        >>> Series(['a', 'b', 'c']).str.cat(['A', 'B', 'C'], sep=',')
        0 a,A
        1 b,B
        2 c,C
        dtype: object
        >>> Series(['a', 'b', 'c']).str.cat(sep=',')
        'a,b,c'
        >>> Series(['a', 'b']).str.cat([['x', 'y'], ['1', '2']], sep=',')
        0    a,x,1
        1    b,y,2
        dtype: object
2、split() 切分字符串
        >>> import numpy,pandas;
        >>> s = pandas.Series(['a_b_c', 'c_d_e', numpy.nan, 'f_g_h'])
        >>> s.str.split('_')
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', -1)
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', 0)
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', 1)
        0    [a, b_c]
        1    [c, d_e]
        2         NaN
        3    [f, g_h]
        dtype: object
        >>> s.str.split('_', 2)
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
        >>> s.str.split('_', 3)
        0    [a, b, c]
        1    [c, d, e]
        2          NaN
        3    [f, g, h]
        dtype: object
3、get() 獲取指定位置的字符串
        >>> s.str.get(0)
        0      a
        1      c
        2    NaN
        3      f
        dtype: object
        >>> s.str.get(1)
        0      _
        1      _
        2    NaN
        3      _
        dtype: object
        >>> s.str.get(2)
        0      b
        1      d
        2    NaN
        3      g
        dtype: object
4、join() 對(duì)每個(gè)字符都用給點(diǎn)的字符串拼接起來,不常用
        >>> s.str.join("!")
        0    a!_!b!_!c
        1    c!_!d!_!e
        2          NaN
        3    f!_!g!_!h
        dtype: object
        >>> s.str.join("?")
        0    a?_?b?_?c
        1    c?_?d?_?e
        2          NaN
        3    f?_?g?_?h
        dtype: object
        >>> s.str.join(".")
        0    a._.b._.c
        1    c._.d._.e
        2          NaN
        3    f._.g._.h
        dtype: object
5、contains() 是否包含表達(dá)式
        >>> s.str.contains('d')
        0    False
        1     True
        2      NaN
        3    False
        dtype: object
6、replace() 替換
        >>> s.str.replace("_", ".")
        0    a.b.c
        1    c.d.e
        2      NaN
        3    f.g.h
        dtype: object
7、repeat() 重復(fù)
        >>> s.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
        dtype: object
8、pad() 左右補(bǔ)齊
>>> s.str.pad(10, fillchar="?")
0    ?????a_b_c
1    ?????c_d_e
2           NaN
3    ?????f_g_h
dtype: object
>>>
>>> s.str.pad(10, side="right", fillchar="?")
0    a_b_c?????
1    c_d_e?????
2           NaN
3    f_g_h?????
dtype: object
9、center() 中間補(bǔ)齊,看例子
>>> s.str.center(10, fillchar="?")
0    ??a_b_c???
1    ??c_d_e???
2           NaN
3    ??f_g_h???
dtype: object
10、ljust() 右邊補(bǔ)齊,看例子
>>> s.str.ljust(10, fillchar="?")
0    a_b_c?????
1    c_d_e?????
2           NaN
3    f_g_h?????
dtype: object
11、rjust() 左邊補(bǔ)齊,看例子
>>> s.str.rjust(10, fillchar="?")
0    ?????a_b_c
1    ?????c_d_e
2           NaN
3    ?????f_g_h
dtype: object
12、zfill() 左邊補(bǔ)0
>>> s.str.zfill(10)
0    00000a_b_c
1    00000c_d_e
2           NaN
3    00000f_g_h
dtype: object
13、wrap() 在指定的位置加回車符號(hào)
>>> s.str.wrap(3)
0    a_b\n_c
1    c_d\n_e
2        NaN
3    f_g\n_h
dtype: object
14、slice() 按給點(diǎn)的開始結(jié)束位置切割字符串
>>> s.str.slice(1,3)
0     _b
1     _d
2    NaN
3     _g
dtype: object
15、slice_replace() 使用給定的字符串,替換指定的位置的字符
>>> s.str.slice_replace(1, 3, "?")
0    a?_c
1    c?_e
2     NaN
3    f?_h
dtype: object
>>> s.str.slice_replace(1, 3, "??")
0    a??_c
1    c??_e
2      NaN
3    f??_h
dtype: object
16、count() 計(jì)算給定單詞出現(xiàn)的次數(shù)
>>> s.str.count("a")
0     1
1     0
2   NaN
3     0
dtype: float64
17、startswith() 判斷是否以給定的字符串開頭
>>> s.str.startswith("a");
0     True
1    False
2      NaN
3    False
dtype: object
18、endswith() 判斷是否以給定的字符串結(jié)束
>>> s.str.endswith("e");
0    False
1     True
2      NaN
3    False
dtype: object
19、findall() 查找所有符合正則表達(dá)式的字符,以數(shù)組形式返回
>>> s.str.findall("[a-z]");
0    [a, b, c]
1    [c, d, e]
2          NaN
3    [f, g, h]
dtype: object
20、match() 檢測(cè)是否全部匹配給點(diǎn)的字符串或者表達(dá)式
>>> s
0    a_b_c
1    c_d_e
2      NaN
3    f_g_h
dtype: object
>>> s.str.match("[d-z]");
0    False
1    False
2      NaN
3     True
dtype: object
21、extract() 抽取匹配的字符串出來,注意要加上括號(hào),把你需要抽取的東西標(biāo)注上
>>> s.str.extract("([d-z])");
0    NaN
1      d
2    NaN
3      f
dtype: object
22、len() 計(jì)算字符串的長(zhǎng)度
>>> s.str.len()
0     5
1     5
2   NaN
3     5
dtype: float64 
23、strip() 去除前后的空白字符
>>> idx = pandas.Series([' jack', 'jill ', ' jesse ', 'frank'])
>>> idx.str.strip()
0     jack
1     jill
2    jesse
3    frank
dtype: object
24、rstrip() 去除后面的空白字符
25、lstrip() 去除前面的空白字符
26、partition() 把字符串?dāng)?shù)組切割稱為DataFrame,注意切割只是切割稱為三部分,分隔符前,分隔符,分隔符后
27、rpartition() 從右切起
>>> s.str.partition('_')
 0    1    2
0    a    _  b_c
1    c    _  d_e
2  NaN  NaN  NaN
3    f    _  g_h
>>> s.str.rpartition('_')
 0    1    2
0  a_b    _    c
1  c_d    _    e
2  NaN  NaN  NaN
3  f_g    _    h
28、lower() 全部小寫
29、upper() 全部大寫
30、find() 從左邊開始,查找給定字符串的所在位置
>>> s.str.find('d')
0    -1
1     2
2   NaN
3    -1
dtype: float64
31、rfind() 從右邊開始,查找給定字符串的所在位置

32、index() 查找給定字符串的位置,注意,如果不存在這個(gè)字符串,那么會(huì)報(bào)錯(cuò)!
33、rindex() 從右邊開始查找,給定字符串的位置
>>> s.str.index('_')
0     1
1     1
2   NaN
3     1
dtype: float64
34、capitalize() 首字符大寫
>>> s.str.capitalize()
0    A_b_c
1    C_d_e
2      NaN
3    F_g_h
dtype: object
35、swapcase() 大小寫互換
>>> s.str.swapcase()
0    A_B_C
1    C_D_E
2      NaN
3    F_G_H
dtype: object
36、normalize() 序列化數(shù)據(jù),數(shù)據(jù)分析很少用到,咱們就不研究了
37、isalnum() 是否全部是數(shù)字和字母組成
>>> s.str.isalnum()
0    False
1    False
2      NaN
3    False
dtype: object
38、isalpha() 是否全部是字母
>>> s.str.isalpha()
0    False
1    False
2      NaN
3    False
dtype: object
39、isdigit() 是否全部都是數(shù)字
>>> s.str.isdigit()
0    False
1    False
2      NaN
3    False
dtype: object
40、isspace() 是否空格
>>> s.str.isspace()
0    False
1    False
2      NaN
3    False
dtype: object
41、islower() 是否全部小寫
42、isupper() 是否全部大寫
>>> s.str.islower()
0    True
1    True
2     NaN
3    True
dtype: object
>>> s.str.isupper()
0    False
1    False
2      NaN
3    False
dtype: object
43、istitle() 是否只有首字母為大寫,其他字母為小寫
>>> s.str.istitle()
0    False
1    False
2      NaN
3    False
dtype: object
44、isnumeric() 是否是數(shù)字
45、isdecimal() 是否全是數(shù)字



本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
如何用pandas對(duì)excel中的文本數(shù)據(jù)進(jìn)行操作
pandas 數(shù)據(jù)規(guī)整
Python 數(shù)據(jù)處理庫(kù) pandas 入門教程
Python Pandas處理字符串(方法詳解)
Pandas | 向量化字符串操作
pandas 分類數(shù)據(jù)處理大全(附代碼)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服