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\]')

猜你喜欢

转载自blog.csdn.net/Ahead_J/article/details/82880361