首页 > 解决方案 > 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)

代码输出仅适用于匹配情况。

标签: regexpandasdataframe

解决方案


尝试:

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 列,这就是您无法获得价值的原因


推荐阅读