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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值