Python string字符串

数据准备

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

大小写

是否小写

[In]:Series.str.islower() #是否小写;str.islower()
[Out]:
0    True
1    True
2    True
3    True
4    True
5    True
dtype: bool

是否大写

[In]:Series.str.isupper() #是否大写;str.isupper()
[Out]:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool

是否首字母大写

[In]:Series.str.istitle() #是否首字母大写;str.istitle()
[Out]:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool

小写

[In]:Series.str.lower() #小写;str.lower()
[Out]:
0      a
1     ab
2    abc
3     bc
4     bb
5      c
dtype: object

大写

[In]:Series.str.upper() #大写 ;str.upper()
[Out]:
0      A
1     AB
2    ABC
3     BC
4     BB
5      C
dtype: object

首字母大写

[In]:Series.str.capitalize() #首字母大写; str.capitalize()
[Out]:
0      A
1     Ab
2    Abc
3     Bc
4     Bb
5      C
dtype: object

只有首字母大写

[In]:Series.str.title() #只大写首字母; str.title()
[Out]:
0      A
1     Ab
2    Abc
3     Bc
4     Bb
5      C
dtype: object

大小写转换

[In]:Series.str.swapcase() #大小写互换;str.swapcase()
[Out]:
0      A
1     AB
2    ABC
3     BC
4     BB
5      C
dtype: object

拼贴

复制拼贴

[In]:Series.str.repeat(2) #复制字符串并且拼贴
[Out]:
0        aa
1      abab
2    abcabc
3      bcbc
4      bbbb
5        cc
dtype: 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 x
1     ab x
2    abc x
3     bc x
4     bb x
5      c x
dtype: object

填充/去空

左右填充

[In]:Series.str.ljust(3,"x") #左移动,右侧填充;Series.str.rjust(width[, fillchar]);str.ljust()
[Out]:
0    axx
1    abx
2    abc
3    bcx
4    bbx
5    cxx
dtype: object

左边0填充

[In]:Series.str.zfill(5) #左边填充; str.zfill()
[Out]:
0    0000a
1    000ab
2    00abc
3    000bc
4    000bb
5    0000c
dtype: object

居中两边填充

[In]:Series.str.center(width=3,fillchar="x") #居中,并用字符填充空白; str.center()
[Out]:
0    xax
1    xab
2    abc
3    xbc
4    xbb
5    xcx
dtype: object

两边填充

[In]:Series.str.pad(3, side='right', fillchar='x') #填充字符
[Out]:
0    axx
1    abx
2    abc
3    bcx
4    bbx
5    cxx
dtype: object

步长填充

[In]:Series.str.join("x") #1个步长包裹;str.join()
[Out]:
0        a
1      axb
2    axbxc
3      bxc
4      bxb
5        c
dtype: object

步长换行

[In]:Series.str.wrap(1) #按步长换行
[Out]:
0          a
1       a\nb
2    a\nb\nc
3       b\nc
4       b\nb
5          c
dtype: object

去除左右空白

[In]:Series.str.lstrip(to_strip=None) #去除左边空白;Series.str.rstrip([to_strip]);str.lstrip();Series.str.strip([to_strip])
[Out]:
0      a
1     ab
2    abc
3     bc
4     bb
5      c
dtype: object

查找

Series索引范围查找

是否包含元素

[In]:Series.str.contains("a") #是否包含元素,可以结合索引得到包含元素的字符;正则
[Out]:
0     True
1     True
2     True
3    False
4    False
5    False
dtype: bool

是否以…开头

[In]:Series.str.startswith("a") #返回是否以a开头;str.startwith()
[Out]:
0     True
1     True
2     True
3    False
4    False
5    False
dtype: bool

是否以…结尾

[In]:Series.str.endswith("c") #返回是否已c结尾; str.endswith()
[Out]:
0    False
1    False
2     True
3     True
4    False
5     True
dtype: bool

匹配

[In]:Series.str.match("b") #正则
[Out]:
0    False
1    False
2    False
3     True
4     True
5    False
dtype: bool

字符串索引查找

返回索引

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

返回索引对应的值

[In]:Series.str.get(1) #返回指定索引的值
[Out]:
0    NaN
1      b
2      b
3      c
4      b
5    NaN
dtype: object
[In]:Series.str.index("bb") #Series.str.rindex(sub[, start, end]);str.index

计数

传入字符的数量

[In]:Series.str.count("b") #计算每个字符包含传入字符的数量
[Out]:
0    0
1    1
2    1
3    1
4    2
5    0
dtype: int64

列元素量

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

统计元素数量

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

分割

分割字符,保留分隔符

[In]:Series.str.partition(pat='b', expand=True) #分割字符,保留分隔符;Series.str.rpartition(pat='b', expand=True)
[Out]:
0   1   2
0   a       
1   a   b   
2   a   b   c
3       b   c
4       b   b
5   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]:
0
0   
1   
2   
3   b
4   bb
5   

extractall

[In]:Series.str.extractall("(b*)")
[Out]:
0
match   
0   0   NaN
1   NaN
1   0   NaN
1   b
2   NaN
2   0   NaN
1   b
2   NaN
3   NaN
3   0   b
1   NaN
2   NaN
4   0   bb
1   NaN
5   0   NaN
1   NaN

根据索引截取字符串

[In]:Series.str.slice(0,1) #根据索引截取字符串
[Out]:
0    a
1    a
2    a
3    b
4    b
5    c
dtype: object

根据索引替换字符串

[In]:Series.str.slice_replace(0,1,"x") #替换指定索引字符串
[Out]:
0      x
1     xb
2    xbc
3     xc
4     xb
5      x
dtype: object

替换传入字符

[In]:Series.str.replace("a","x") #替换; str.replace(); re.sub()
[Out]:
0      x
1     xb
2    xbc
3     bc
4     bb
5      c
dtype: object

字典转换

[In]:tb = str.maketrans("abc", "123") #翻译;str.translate()
Series.str.translate(tb)
[Out]:
0      1
1     12
2    123
3     23
4     22
5      3
dtype: object

其他

[In]:Series.str.encode("utf-8").str.decode("GBK") #bytes.decode();str.encode()
[Out]:
0      a
1     ab
2    abc
3     bc
4     bb
5      c
dtype: object
[In]:Series.str.len() #查看每个字符串的长度
[Out]:
0    1
1    2
2    3
3    2
4    2
5    1
dtype: int64
[In]:Series.str.normalize("NFC") #unicodedata.normalize()
[Out]:
0      a
1     ab
2    abc
3     bc
4     bb
5      c
dtype: object
[In]:Series.str.isalnum() #是否是字母和数字;str.isalnum()
[Out]:
0    True
1    True
2    True
3    True
4    True
5    True
dtype: bool
[In]:Series.str.isalpha() #是否是字母;str.isalpha()
[Out]:
0    True
1    True
2    True
3    True
4    True
5    True
dtype: bool
[In]:Series.str.isdigit() #是否是数字;str.isdigit()
[Out]:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool
[In]:Series.str.isspace() #是否是空白;str.isspace()
[Out]:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool
[In]:Series.str.isnumeric() #是否是数字;str.isnumeric()
[Out]:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool
[In]:Series.str.isdecimal() #是否是小数;str.isdecimal()
[Out]:
0    False
1    False
2    False
3    False
4    False
5    False
dtype: bool
[In]:Series.str.get_dummies(sep=',') #矩阵
[Out]:
    a   ab  abc bb  bc  c
0   1   0   0   0   0   0
1   0   1   0   0   0   0
2   0   0   1   0   0   0
3   0   0   0   0   1   0
4   0   0   0   1   0   0
5   0   0   0   0   0   1
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值