1. replace
Series.str.replace
(pat, repl, n=-1, case=None, flags=0, regex=True)
Parameters: | pat : string or compiled regex
repl : string or callable
n : int, default -1 (all)
case : boolean, default None
flags : int, default 0 (no flags)
regex : boolean, default True
|
---|---|
Returns: | replaced : Series/Index of objects |
Raises: | ValueError
|
pat: 可以是字符串或者正则表达式
# pat = string
# 匹配字符串并替换
>>> pd.Series(['f.o', 'fuz', np.nan]).str.replace('f.', 'ba', regex=False)
0 bao
1 fuz
2 NaN
dtype: object
>>> pd.Series(['abc', 'deec']).str.replace('c', '', regex=False)
0 ab
1 dee
dtype: object
>>> pd.Series(['abca', 'deeca']).str.replace('ca', '', regex=False)
0 ab
1 dee
dtype: object
# pat = regex
# 用正则表达式进行字符串匹配
>>> pd.Series(['f (oo)', 'buo', 'sio']).str.replace('\s*\(\w*\)', '', regex=True)
0 f
1 buo
2 sio
dtype: object
2. match
Series.str.
match
(pat, case=True, flags=0, na=nan, as_indexer=None)- 对Series的每个值匹配正则表达式,并返回True/False
>>>ut.RegionName.str.match('[\w\s]*\[edit\]')