Python pandas.Series.str
阿新 • • 發佈:2018-11-23
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\]')