大小写
大小写转换、首字母大写、英文书写格式、大小写反转的函数。pandas.Series.str.capitalize
file:[例1]
ser = pd.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
print(ser.str.lower())
"""
0 lower
1 capitals
2 this is a sentence
3 swapcase
dtype: object
"""
print(ser.str.upper())
"""
0 LOWER
1 CAPITALS
2 THIS IS A SENTENCE
3 SWAPCASE
dtype: object
"""
print(ser.str.title())
"""
0 Lower
1 Capitals
2 This Is A Sentence
3 Swapcase
dtype: object
"""
print(ser.str.capitalize())
"""
0 Lower
1 Capitals
2 This is a sentence
3 Swapcase
dtype: object
"""
print(ser.str.swapcase())
"""
0 LOWER
1 capitals
2 THIS IS A SENTENCE
3 sWaPcAsE
dtype: object
"""
casefold
移除字符串中的所有大小写区别。等同于 lower()
。pandas.Series.casefold
file:[例2]
ser = pd.Series(['lower', 'CAPITALS', 'this is a sentence', 'SwApCaSe'])
print(ser.str.casefold())
"""
0 lower
1 capitals
2 this is a sentence
3 swapcase
dtype: object
"""
查询
contains
测试字符串或正则表达式是否包含在 Series 的字符串中,如例3所示,每一个字符串都包含了 XL。pandas.Series.str.contains
file:[例3]
data = pd.read_excel(rf'C:\examples.xlsx')
print(data.loc[data['产品编码'].str.contains(pat='XL')]['产品编码'])
"""
1 XL03ACR107B001030204021
2 XL03ACR107B014020104870
4 XL01EBJ008A001010105847
5 XL01XGP043B001020104021
10 XL01EAY059A001010105847
...
2354 XR06AXL226A001010405848
2361 XL04AAL019A001010104859
2409 XR06AXL226A001010405848
2440 XR06AXL226K001010101442
2479 XR06AXL226A001010405848
Name: 产品编码, Length: 100, dtype: object
"""
fullmatch
确定字符串或正则表达式是否完全在 Series 的字符串中,返回一个布尔 Series。pandas.Series.str.fullmatch
file:[例4]
print(data.loc[data['产品编码'].str.fullmatch(pat='XL')]['产品编码'])
"""
Series([], Name: 产品编码, dtype: object)
"""
如例4所示,XL 不完全匹配产品编码的字符串,即空。修改正则表达式,匹配范围更广一点:
file:[例5]
print(data.loc[data['产品编码'].str.fullmatch('.*XL.*')]['产品编码'])
"""
1 XL03ACR107B001030204021
2 XL03ACR107B014020104870
4 XL01EBJ008A001010105847
5 XL01XGP043B001020104021
10 XL01EAY059A001010105847
...
2354 XR06AXL226A001010405848
2361 XL04AAL019A001010104859
2409 XR06AXL226A001010405848
2440 XR06AXL226K001010101442
2479 XR06AXL226A001010405848
Name: 产品编码, Length: 100, dtype: object
"""
fm = data.loc[data['产品编码'].str.fullmatch('.*XL.*')]['产品编码']
co = data.loc[data['产品编码'].str.contains('.*XL.*')]['产品编码']
print(fm.equals(co))
"""
True
"""
例3与例5的结果和数量都一致。
findall
在 Series 中查找正则表达式的所有匹配项。pandas.Series.str.findall
file:[例6]
data = pd.read_excel(rf'C:\examples.xlsx')
print(data['产品编码'].str.findall(r'[\d]+'))
"""
0 [10, 247, 002010103546]
1 [03, 107, 001030204021]
2 [03, 107, 014020104870]
3 [01, 112, 010030105198]
4 [01, 008, 001010105847]
...
2522 [10, 079, 002010207103]
2523 [10, 080, 002010107103]
2524 [10, 074, 002030100227]
2525 [10, 087, 002010107103]
2526 [10, 025, 002020181708]
Name: 产品编码, Length: 2527, dtype: object
"""
findall
查询的是匹配正则表达式中匹配的项,例如 XL03ACR107B001030204021
就会得到一数组 [10, 247, 002010103546]
。
endswith 和 startswith
- endswith:查找在字符串中是否包含指定结尾的子字符串。不接受正则表达式。
- startswith:与 endswith 相反,查找开头的子字符串。
file:[例7]
ser = pd.Series(['bat', 'bear', 'caT', np.nan])
print(ser.str.endswith('t'))
"""
0 True
1 False
2 False
3 NaN
dtype: object
"""
print(ser.str.startswith('b'))
"""
0 True
1 False
2 False
3 NaN
dtype: object
"""
这两个函数可以结合 loc
来做索引 DataFrame 的操作,例如:df.loc[df['产品编码'].str.startswith('XL')]
。
count
计算 Series 中每个字符串中的字符出现的次数。
file:[例8]
ser = pd.Series(['A', 'B', 'Aaba', 'Baca', np.nan, 'CABA', 'cat'])
print(ser.str.count('a'))
"""
0 0.0
1 0.0
2 2.0
3 2.0
4 NaN
5 0.0
6 1.0
dtype: float64
"""
文本处理
center、ljust、rjust
具体查看官方示例文档 pandas.Series.str.center。
- center:左右两边填充字符到指定长度
- ljust:右填充字符到指定长度
- rjust:左填充字符到指定长度
这三个函数还与 pandas.Series.str.pad 函数、pandas.Series.str.zfill 函数类似。
strip、lstrip、rstrip
具体查看官方示例文档 pandas.Series.str.strip。
- strip:清除首尾字符
- lstrip:清除首字符
- rstrip:清除尾字符
参数 to_strip
指的是:指定删除的字符集,此字符集的所有组合都将被剥离为数组。若 None 或不传递,则删除空格。
file:[例9]
ser = pd.Series(['1. Ant. ', '2. Bee!\n', '3. Cat?\t', np.nan, 10, True])
ser.str.lstrip('123.')
"""首
0 Ant.
1 Bee!\n
2 Cat?\t
3 NaN
4 NaN
5 NaN
dtype: object
"""
ser.str.rstrip('.!? \n\t')
"""尾
0 1. Ant
1 2. Bee
2 3. Cat
3 NaN
4 NaN
5 NaN
dtype: object
"""
ser.str.strip('123.!? \n\t')
"""首尾
0 Ant
1 Bee
2 Cat
3 NaN
4 NaN
5 NaN
dtype: object
"""
与 lstrip 和 rstrip 相似的还有 removeprefix 和 removesuffix。
replace
替换 Series 中每次出现的 pattern 或者 regex。
file:[例10]
pd.Series(['foo', 'fuz', np.nan]).str.replace('f.', 'ba', regex=True)
"""
0 bao
1 baz
2 NaN
dtype: object
"""
pd.Series(['f.o', 'fuz', np.nan]).str.replace('f.', 'ba', regex=False)
"""
0 bao
1 fuz
2 NaN
dtype: object
"""
若要用正则表达式进行替换,需给 regex 传递 True,不然就会被当作为 patter 进行替换。如例10中的第二个,f.o
被替换为 bao
,因为 regex 为 False,patter 为 f.
。