regex - python数据框中正则表达式识别数字的操作
问题描述
我有一个包含 2 列的数据框,第 3 列是下面给出的输出格式:
东风:
reg value o/p** 2 for $20 11 20/2 4 for $24 12 24/4 2 for $30 13 30/2 Get $10 Cash 14 14 3 for $30 21 30/3
首先,我必须在 reg 列中为 [$][\d]+ 匹配 [\d]+ ,然后如果没有匹配项保持相同的值,则必须将值列更新为 reg 的第二个整数除以 reg 的第一个整数。
我的代码是:
df["value"]=df["reg"].map(lambda x: (int(re.findall("[\d]+",x)[1]))/int(re.findall("[\d]+",x)[0]) if(re.search(r"[\d]+ for [$][\d]+" , x)) else x)
代码输出仅适用于匹配情况。
解决方案
尝试:
df["value"]=df.apply(lambda x: (int(re.findall("[\d]+",x["reg"])[1]))/int(re.findall("[\d]+",x["reg"])[0]) if(re.search(r"[\d]+ for [$][\d]+" , x["reg"])) else x["value"], axis=1)
输出:
reg value
0 2 for $20 10.0
1 4 for $24 6.0
2 2 for $30 15.0
3 Get $10 Cash 14.0
4 3 for $30 10.0
您只选择 reg 列,这就是您无法获得价值的原因
推荐阅读
- ssl - 无法在 python2.6 中使用 PIP 更新/添加任何包
- dynamics-crm - 汇总字段十进制抛出错误
- pdf - 是否可以将fabricjs svg输出转换为pdf而不进行光栅化?
- python - PyCharm 在未选中检查时会出现代码兼容性错误
- swift - 设置语言环境时UIDatePicker错误的时间位置
- r - 根据共享值合并两个数据框
- ios - UIImagePickerController 显示带有灰色背景的专辑列表行
- json - 如何修复 json 中的换行错误:非法未引用字符 ... 必须使用反斜杠进行转义
- mongodb - 引用集合的归档而不是集合 mongodb
- php - 如何在此代码中的页面之间传递参数?