python - Extracting US dollar amount
问题描述
This question has been asked before but I am still not able to make this work entirely. I have the following examples of strings:
"Transfer to Retirement Rsvs-MA FX .11"
"Opening Balance FX 342,536,002.63"
"VA 85.85"
"VB .00"
"Manual Adjustment FX 6,838.36-"
I would like to extract the US dollar/cents amount from the strings into a separate column of a dataframe. I have the following regex expression:
rx = (r"(\$?(?:\d+,)*\d+\.\d+\-?)")
and I tried to create a column in the dataframe (df) called "dollars"
df2['dollars']=df2['description'].str.extract(rx)
It works for the most part, except for values like .11 or .00, in which case nan is returned. How do I revise this expression to make it work for cents without leading dollars?
Help with this is greatly appreciated!
string dollars
Transfer to Retirement Rsvs-MA FX .11 0.11
Opening Balance FX 342,536,002.63 342,536,002.63
VA 85.85 85.85
VB .00 .00
Manual Adjustment FX 6,838.36- 6,836-
解决方案
You might use:
(?<!\S)\$?(?:\d{1,3}(?:\,\d{3})*)?\.\d{2}-?(?!\S)
(?<!\S)
Whitespace boundary on the left\$?
Optional dollar sign(?:\d{1,3}(?:\,\d{3})*)?
Optional part matching 1-3 digits optionally repeated by comma and 3 digits\.\d{2}
Match a.
and 2 digits-?
Optional hyphen(?!\S)
Whitespace boundary on the right
If you want 1+ digits after the dot, change \.\d{2}
to \.\d+
推荐阅读
- javascript - 错误:WebGL 警告:texImage2D:所需上传的数据比可用数据多:(加载带有三角形网格数据和法线的纹理时)
- android - 从内容 uri 读取文件时出现权限错误
- makefile - 如果函数中的条件 - Makefile
- c# - 如何在我的 c# android xamarin 项目中使用 google firebase
- c# - 缩写列表中给出的字符串
- python - 来自 SQLAlchemy 的加密数据库连接
- jquery - 如何通过拖放事件将带有附加元素的图像保存到数据库中,并显示带有与拖放相同位置的附加元素的图像
- r - 尽管没有返回错误,为什么我的 princomp 情节为空?
- c# - 使用 C# ASP.NET MVC Identity 以编程方式创建用户
- excel - vba excel:如何将字符从一个单元格复制到另一个单元格