熊猫正则表达式匹配字符串从左到右使用另一列

时间:2018-09-07 10:36:25

标签: python python-3.x pandas dataframe

我有一个df,我想从amount的左开始匹配inv_id,并创建一个布尔列left_matchTrue-如果存在匹配项,False-反之,

amount    inv_id        
309.9     30990071218
3130.0    313000B20180501
3330.50   3330.5020180425
17.35     13249261 100117
43.2      9037878 020418

我将首先从inv_id中删除所有非数字字符,

s = df[inv_id].str.replace(r'\D+', '')

然后将amount * 100转换为字符串,

df['amt_str'] = (df['amount']*100).round().astype(int).astype(str) 

我想知道如何使用amt_str来匹配s。结果应该看起来像

amount    inv_id             left_match        
309.9     30990071218        True
3130.0    313000B20180501    True
3330.50   3330.5020180425    True
17.35     13249261 100117    False
43.2      9037878 020418     False

3 个答案:

答案 0 :(得分:1)

df['left_match'] = df.apply(lambda x:True if re.search(str(x['amount']).split('.')[0],x['inv_id']).start() ==0 else False,axis=1)
df

输出:

amount  inv_id  left_match
0   309.90  30990071218 True
1   3130.00 313000B20180501 True
2   17.35   13249261 100117 False

答案 1 :(得分:1)

我认为您需要:

var renederedContent = template.Render(new {
    model = new {
        Data = "some string"
    },
    m => m.Name
});

答案 2 :(得分:1)

df['inv_id_numeric'] = df['inv_id'].str.replace(r'\D+', '')
df['amt_str'] = (df['amount']*100).round().astype(int).astype(str)
df['left_match'] = df.apply(lambda x: x['inv_id_numeric'].startswith(x['amt_str']), axis=1)

输出:

    amount           inv_id  inv_id_numeric amt_str  left_match
0   309.90      30990071218     30990071218   30990        True
1  3130.00  313000B20180501  31300020180501  313000        True
2  3330.50  3330.5020180425  33305020180425  333050        True
3    17.35  13249261 100117  13249261100117    1735       False
4    43.20   9037878 020418   9037878020418    4320       False