导图社区 Series.str详细
python中pandas模块对Series的str方法大全
编辑于2022-03-01 23:07:14Series.str 详细
数据源
模块: pandas,re
streg=pd.Series(['Tomato','TOKYO','tyboys','2只tigers','5201314','123 abc',' ',None],name='strEG')
变量名: eg=streg.str
数据展示
0 Tomato 1 TOKYO 2 tyboys 3 2只tigers 4 5201314 5 123 abc 6 7 None Name: strEG, dtype: object
字符填充/消除
万能填充
填充
eg.pad(width=10,fillchar='^',side='left') #side: 可选left,right,both
0 ^^^^Tomato 1 ^^^^^TOKYO 2 ^^^^tyboys 3 ^^2只tigers 4 ^^^5201314 5 ^^^123 abc 6 ^^^^^^^^^ 7 None Name: strEG, dtype: object
左填充/消除
填充
rjust=eg.rjust(width=10,fillchar='^')
eg.zfill(8) #左填0
消除
rjust.str.lstrip(to_strip='^')
右填充/消除
填充
ljust=eg.ljust(width=10,fillchar='^')
消除
ljust.str.rstrip(to_strip='^')
中间填充/消除
填充
center=eg.center(width=10,fillchar='^')
消除
center.str.strip(to_strip='^')
字符查找
查找判断->True/False
#包含 eg.contains(pat=r't.*s',flags=re.IGNORECASE)
#开头(可正则) eg.match(pat=r't.*s',flags=re.IGNORECASE)
#全部 eg.fullmatch(pat=r'(t.*[os])|(\d*)',flags=re.IGNORECASE,na="hahahha")
0 True 1 True 2 True 3 False 4 True 5 False 6 False 7 hahahha Name: strEG, dtype: object
#开头(不可正则) eg.startswith(pat='T',na='lala')
#结尾(不可正则) eg.endswith(pat='s')
可正则查找
#提取内容, 正则图式要分组 eg.extract(pat=r'(t.*?o)',flags=re.IGNORECASE,expand=True)
eg.extractall(pat=r'(t.*?o)',flags=re.IGNORECASE)
eg.findall(pat=r't.*?o',flags=re.IGNORECASE)
0 [To, to] 1 [TO] 2 [tybo] 3 [] 4 [] 5 [] 6 [] 7 None Name: strEG, dtype: object
eg.count(pat=r't.*?o',flags=re.IGNORECASE)
0 2.0 1 1.0 2 1.0 3 0.0 4 0.0 5 0.0 6 0.0 7 NaN Name: strEG, dtype: float64
不可正则查找
eg.find('t')
0 4.0 1 -1.0 2 0.0 3 2.0 4 -1.0 5 -1.0 6 -1.0 7 NaN Name: strEG, dtype: float64
#从右边找 eg.rfind('o')
# 索引字符,不太好用,不如slice eg.get(1)
返回不了就会报错系列
#streg为Series名 streg[[0,2]].str.index('o')
0 1 2 3 Name: strEG, dtype: int64
#右边查找 streg[[0,2]].str.rindex('o')
其他操作
字符长度
eg.len()
插入符号
#每个cell 为一个迭代器,字符串形式 eg.join('-')
0 T-o-m-a-t-o 1 T-O-K-Y-O 2 t-y-b-o-y-s 3 2-只-t-i-g-e-r-s 4 5-2-0-1-3-1-4 5 1-2-3- -a-b-c 6 7 None Name: strEG, dtype: object
#列表形式 egFindall=eg.findall(pat=r'.o',flags=re.IGNORECASE) egFindall.str.join('-')
0 To-to 1 TO-YO 2 bo 3 4 5 6 7 None Name: strEG, dtype: object
重复
eg.repeat(2)
长字符串分段
for each in eg.wrap(width=3)[:2]: print(each)
Tom ato TOK YO
切片拆分
切片操作
eg.slice(1,5,2) eg[1:5:2]
0 oa 1 OY 2 yo 3 只i 4 21 5 2 6 7 None Name: strEG, dtype: object
拆分
eg.split(pat='o',expand=True)
#从右边切 eg.rsplit(pat='o',expand=True)
拆成三份
eg.partition(sep='o',expand=True)
eg.rpartition(sep='o',expand=True)
其他
eg2=pd.Series(['a,c,132','qw,cvs']).str eg2.get_dummies(sep=',')
字符串替换
正则替换
eg.replace(pat=r't.*?o',repl='\^.^/',regex=True,flags=re.IGNORECASE)
0 \^.^/ma\^.^/ 1 \^.^/KYO 2 \^.^/ys 3 2只tigers 4 5201314 5 123 abc 6 7 None Name: strEG, dtype: object
位置替换
eg.slice_replace(start=1,stop=5,repl=':)')
0 T:)o 1 T:) 2 t:)s 3 2:)ers 4 5:)14 5 1:)bc 6 :) 7 None Name: strEG, dtype: object
字母替换
trans=str.maketrans({'t':'z','o':'q'}) eg.translate(trans)
0 Tqmazq 1 TOKYO 2 zybqys 3 2只zigers 4 5201314 5 123 abc 6 7 None Name: strEG, dtype: object
编码
编码
ecd=eg.encode(encoding='gbk')
0 b'Tomato' 1 b'TOKYO' 2 b'tyboys' 3 b'2\xd6\xbbtigers' 4 b'5201314' 5 b'123 abc' 6 b' ' 7 None Name: strEG, dtype: object
解码
ecd.str.decode(encoding='gbk')
0 Tomato 1 TOKYO 2 tyboys 3 2只tigers 4 5201314 5 123 abc 6 7 None Name: strEG, dtype: object
不懂
eg.normalize(form='NFC')
字符类型判断
字母数字
eg.isalnum()
字母
eg.isalpha()
数字
eg.isdecimal() #10分位数
eg.isdigit() #支持特殊编码的数字
eg.isnumeric() #除了特殊编码外,也支持分数
空格
eg.isspace() #全空格/制表符/分行符
字符大小写
开头大写
eg.capitalize()
eg.title()
eg.istitle() #判断
大写
eg.upper()
eg.isupper() #判断
小写
eg.lower()
eg.casefold()
eg.islower() #判断
大小写转换
eg.swapcase()
Let's go!
作用
字符大小写
字符类型判断
字符填充/消除
编码
字符查找
字符串替换
切片拆分
其他操作