Python string字符串

来源:互联网 发布:淘宝开店需要什么要求 编辑:程序博客网 时间:2024/06/04 19:52

数据准备

import pandas as pdSeries=pd.Series(["a","ab","abc","bc","bb","c"])

大小写

是否小写

[In]:Series.str.islower() #是否小写;str.islower()[Out]:0    True1    True2    True3    True4    True5    Truedtype: bool

是否大写

[In]:Series.str.isupper() #是否大写;str.isupper()[Out]:0    False1    False2    False3    False4    False5    Falsedtype: bool

是否首字母大写

[In]:Series.str.istitle() #是否首字母大写;str.istitle()[Out]:0    False1    False2    False3    False4    False5    Falsedtype: bool

小写

[In]:Series.str.lower() #小写;str.lower()[Out]:0      a1     ab2    abc3     bc4     bb5      cdtype: object

大写

[In]:Series.str.upper() #大写 ;str.upper()[Out]:0      A1     AB2    ABC3     BC4     BB5      Cdtype: object

首字母大写

[In]:Series.str.capitalize() #首字母大写; str.capitalize()[Out]:0      A1     Ab2    Abc3     Bc4     Bb5      Cdtype: object

只有首字母大写

[In]:Series.str.title() #只大写首字母; str.title()[Out]:0      A1     Ab2    Abc3     Bc4     Bb5      Cdtype: object

大小写转换

[In]:Series.str.swapcase() #大小写互换;str.swapcase()[Out]:0      A1     AB2    ABC3     BC4     BB5      Cdtype: object

拼贴

复制拼贴

[In]:Series.str.repeat(2) #复制字符串并且拼贴[Out]:0        aa1      abab2    abcabc3      bcbc4      bbbb5        ccdtype: object

拼贴特定字符

[In]:Series.str.cat(sep=" ") #拼贴字符,可以对自己拼贴成字符串,也可以添加其他的字符[Out]:'a ab abc bc bb c'[In]:Series.str.cat(["x","x","x","x","x","x"],sep=" ")[Out]:0      a x1     ab x2    abc x3     bc x4     bb x5      c xdtype: object

填充/去空

左右填充

[In]:Series.str.ljust(3,"x") #左移动,右侧填充;Series.str.rjust(width[, fillchar]);str.ljust()[Out]:0    axx1    abx2    abc3    bcx4    bbx5    cxxdtype: object

左边0填充

[In]:Series.str.zfill(5) #左边填充; str.zfill()[Out]:0    0000a1    000ab2    00abc3    000bc4    000bb5    0000cdtype: object

居中两边填充

[In]:Series.str.center(width=3,fillchar="x") #居中,并用字符填充空白; str.center()[Out]:0    xax1    xab2    abc3    xbc4    xbb5    xcxdtype: object

两边填充

[In]:Series.str.pad(3, side='right', fillchar='x') #填充字符[Out]:0    axx1    abx2    abc3    bcx4    bbx5    cxxdtype: object

步长填充

[In]:Series.str.join("x") #1个步长包裹;str.join()[Out]:0        a1      axb2    axbxc3      bxc4      bxb5        cdtype: object

步长换行

[In]:Series.str.wrap(1) #按步长换行[Out]:0          a1       a\nb2    a\nb\nc3       b\nc4       b\nb5          cdtype: object

去除左右空白

[In]:Series.str.lstrip(to_strip=None) #去除左边空白;Series.str.rstrip([to_strip]);str.lstrip();Series.str.strip([to_strip])[Out]:0      a1     ab2    abc3     bc4     bb5      cdtype: object

查找

Series索引范围查找

是否包含元素

[In]:Series.str.contains("a") #是否包含元素,可以结合索引得到包含元素的字符;正则[Out]:0     True1     True2     True3    False4    False5    Falsedtype: bool

是否以…开头

[In]:Series.str.startswith("a") #返回是否以a开头;str.startwith()[Out]:0     True1     True2     True3    False4    False5    Falsedtype: bool

是否以…结尾

[In]:Series.str.endswith("c") #返回是否已c结尾; str.endswith()[Out]:0    False1    False2     True3     True4    False5     Truedtype: bool

匹配

[In]:Series.str.match("b") #正则[Out]:0    False1    False2    False3     True4     True5    Falsedtype: bool

字符串索引查找

返回索引

[In]:Series.str.find("b") #返回字符串的最低的索引,可是设置查询的起点和终点;Series.str.rfind(sub[, start, end]);str.find()[Out]:0   -11    12    13    04    05   -1dtype: int64

返回索引对应的值

[In]:Series.str.get(1) #返回指定索引的值[Out]:0    NaN1      b2      b3      c4      b5    NaNdtype: object[In]:Series.str.index("bb") #Series.str.rindex(sub[, start, end]);str.index

计数

传入字符的数量

[In]:Series.str.count("b") #计算每个字符包含传入字符的数量[Out]:0    01    12    13    14    25    0dtype: int64

列元素量

[In]:Series.count() #相当于len()[Out]:6

统计元素数量

[In]:Series.value_counts() #计算值的出现次数[Out]:c      1bc     1a      1ab     1abc    1bb     1dtype: int64

分割

分割字符,保留分隔符

[In]:Series.str.partition(pat='b', expand=True) #分割字符,保留分隔符;Series.str.rpartition(pat='b', expand=True)[Out]:0   1   20   a       1   a   b   2   a   b   c3       b   c4       b   b5   c       

分割字符

[In]:Series.str.split("b") #分割字符串;Series.str.rsplit([pat, n, expand]);str.split()[Out]:0       [a]1     [a, ]2    [a, c]3     [, c]4    [, , ]5       [c]dtype: object

截取/替换

返回所查找字符

[In]:Series.str.findall("b") #返回所有的要查找的字符;re.findall()[Out]:0        []1       [b]2       [b]3       [b]4    [b, b]5        []dtype: object

返回附一个匹配的查找字符

[In]:Series.str.extract("(b*)",expand=True) #返回第一个匹配到的目标,正则[Out]:00   1   2   3   b4   bb5   

extractall

[In]:Series.str.extractall("(b*)")[Out]:0match   0   0   NaN1   NaN1   0   NaN1   b2   NaN2   0   NaN1   b2   NaN3   NaN3   0   b1   NaN2   NaN4   0   bb1   NaN5   0   NaN1   NaN

根据索引截取字符串

[In]:Series.str.slice(0,1) #根据索引截取字符串[Out]:0    a1    a2    a3    b4    b5    cdtype: object

根据索引替换字符串

[In]:Series.str.slice_replace(0,1,"x") #替换指定索引字符串[Out]:0      x1     xb2    xbc3     xc4     xb5      xdtype: object

替换传入字符

[In]:Series.str.replace("a","x") #替换; str.replace(); re.sub()[Out]:0      x1     xb2    xbc3     bc4     bb5      cdtype: object

字典转换

[In]:tb = str.maketrans("abc", "123") #翻译;str.translate()Series.str.translate(tb)[Out]:0      11     122    1233     234     225      3dtype: object

其他

[In]:Series.str.encode("utf-8").str.decode("GBK") #bytes.decode();str.encode()[Out]:0      a1     ab2    abc3     bc4     bb5      cdtype: object[In]:Series.str.len() #查看每个字符串的长度[Out]:0    11    22    33    24    25    1dtype: int64[In]:Series.str.normalize("NFC") #unicodedata.normalize()[Out]:0      a1     ab2    abc3     bc4     bb5      cdtype: object[In]:Series.str.isalnum() #是否是字母和数字;str.isalnum()[Out]:0    True1    True2    True3    True4    True5    Truedtype: bool[In]:Series.str.isalpha() #是否是字母;str.isalpha()[Out]:0    True1    True2    True3    True4    True5    Truedtype: bool[In]:Series.str.isdigit() #是否是数字;str.isdigit()[Out]:0    False1    False2    False3    False4    False5    Falsedtype: bool[In]:Series.str.isspace() #是否是空白;str.isspace()[Out]:0    False1    False2    False3    False4    False5    Falsedtype: bool[In]:Series.str.isnumeric() #是否是数字;str.isnumeric()[Out]:0    False1    False2    False3    False4    False5    Falsedtype: bool[In]:Series.str.isdecimal() #是否是小数;str.isdecimal()[Out]:0    False1    False2    False3    False4    False5    Falsedtype: bool[In]:Series.str.get_dummies(sep=',') #矩阵[Out]:    a   ab  abc bb  bc  c0   1   0   0   0   0   01   0   1   0   0   0   02   0   0   1   0   0   03   0   0   0   0   1   04   0   0   0   1   0   05   0   0   0   0   0   1
0 0
原创粉丝点击