1. 程式人生 > >Python pandas.Series.str

Python pandas.Series.str

1. replace

  • Series.str.replace(patrepln=-1case=Noneflags=0regex=True)
Parameters:

pat : string or compiled regex

String can be a character sequence or regular expression.

New in version 0.20.0: pat also accepts a compiled regex.

repl : string or callable

Replacement string or a callable. The callable is passed the regex match object and must return a replacement string to be used. See re.sub().

New in version 0.20.0: repl also accepts a callable.

n : int, default -1 (all)

Number of replacements to make from start

case : boolean, default None

  • If True, case sensitive (the default if pat is a string)
  • Set to False for case insensitive
  • Cannot be set if pat is a compiled regex

flags : int, default 0 (no flags)

  • re module flags, e.g. re.IGNORECASE
  • Cannot be set if pat is a compiled regex

regex : boolean, default True

  • If True, assumes the passed-in pattern is a regular expression.
  • If False, treats the pattern as a literal string
  • Cannot be set to False if pat is a compiled regex or repl is a callable.

New in version 0.23.0.

Returns:

replaced : Series/Index of objects

Raises:

ValueError

  • if regex is False and repl is a callable or pat is a compiled regex
  • if pat is a compiled regex and case or flags is set

 

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(patcase=Trueflags=0na=nanas_indexer=None)
  • 對Series的每個值匹配正則表示式,並返回True/False
>>>ut.RegionName.str.match('[\w\s]*\[edit\]')