pandas 使用正则表达式替换值 [英] pandas applying regex to replace values

查看:78
本文介绍了 pandas 使用正则表达式替换值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将一些定价数据读入pandas数据框中,其值显示为:

I have read some pricing data into a pandas dataframe the values appear as:

$40,000*
$40000 conditions attached

我想将其简化为数字值. 我知道我可以遍历并应用正则表达式

I want to strip it down to just the numeric values. I know I can loop through and apply regex

[0-9]+

在每个字段中然后将结果列表重新组合在一起,但是有没有一种循环的方法?

to each field then join the resulting list back together but is there a not loopy way?

谢谢

推荐答案

您可以使用re.sub()删除所有非数字:

You could remove all the non-digits using re.sub():

value = re.sub(r"[^0-9]+", "", value)

regex101演示

这篇关于 pandas 使用正则表达式替换值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆