pandas str字符串处理

object str

Object str is defined as a attribute of a Seires or a Index,
specifically used to handle the textual content.

s.str

Common method

s.str[n] # serial indexer

Split

s.str.split(regex, n, expand)
s.str.rsplit(regex, n, expand)

regex is a regular expression as a seperator.
n is the max num of split operations.
expand is a bool value, indicating whether expands result as many columns.

Merge

s.str.join(sep)
s1.str.cat(s2, sep, na_rep, join)

sep is a str as seperator.
na_rep is a str to replace NaN.
join is the method of joining.

Match

s.str.contains(regex) # return bool value
s.str.startswith(regex)
s.str.endswith(regex)
s.str.find(string) # return index
s.str.rfind(string)

Replace

s.str.replace(regex, value)
s.str.replace(regex, func)
s.str.replace(string, value, regex=False)

regex is a regular expression.
value is the alternate value.
func, inputs a re.Match object, outputs a string.

Extract

s.str.extract(regex) # return a df
s.str.extractall(regex)
s.str.findall(regex) # return a series

Letter processing

s.str.upper()
s.str.lower()
s.str.title()
s.str.capitalize()
s.str.swapcase()

Numeric operation

pd.to_numeric(s, errors, downcast)

s is a ps.Series.
errors is a str in [‘raise’, ‘coerce’, ‘ignore’],
indicating how to process error.
downcast is a str in [‘integer’, ‘signed’, ‘unsigned’, ‘float’].

Statistics

s.str.len()
s.str.count(regex)

Formatting

s.str.strip()
s.str.rstrip()
s.str.lstrip()
s.str.pad(num, direction, padding)

num is the min length.
direction is a string in [‘right’, ‘left’, ‘both’],
indicating the padding direction.\

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值