1

How to match data column with its own regex in another column. Here is how it looks like.

        Data            Regex
0   HU13568812       ^HU[0-9]{8}
1   78567899         ^NO[0-9]{5}
2   AT1234567        ^HU[0-9]{7}

The output will be a new column for the result if its match (1) or not match (0) like this.

            Data            Regex     Match
0   HU13568812       ^HU[0-9]{8}        1
1   78567899         ^NO[0-9]{5}        0
2   AT1234567        ^AT[0-9]{7}        1

I tried to use re.match() but I can't seem to get it match for the whole row at once. Is there any better way to do this in a simple function or more pythonic way? Thank you.

1
  • Please show the code, how you used it. Note that re.match only matches at the start of string and does not anchor the match at the end of string. Can you change Regex column values by adding $ at the end? Commented Apr 14, 2020 at 9:30

2 Answers 2

3

One way is to use list comprehension:

import re

df["Match"] = [1 if re.search(fr"{pat}", data) else 0 
               for data, pat in zip(df["Data"],df["Regex"])]

print (df)

         Data        Regex  Match
0  HU13568812  ^HU[0-9]{8}      1
1    78567899  ^NO[0-9]{5}      0
2   AT1234567  ^AT[0-9]{7}      1
Sign up to request clarification or add additional context in comments.

Comments

1

Your question may like this Add additional column in merged csv file you can do your own logic, it's very flexible

def func(row):
     # do your generate logic here,and return
    return re.match(row["Regex"],row["Data"]) and 1 or 0
df["Match"]=df.apply(func, axis=1)
print(df)

#lambda for short
df["Match"]=df.apply(lambda r:re.match(r["Regex"],r["Data"]) and 1 or 0, axis=1)

4 Comments

what if the regex column is in another dataframe but still linked to another column 'Country'. so in the data df it also linked with the 'Country' column. is there any way to do the matching using lambda for 2 different dataframe?
lambda is just for brevity, you can use a normal function. what is your logic , pls show an example. there is not a 'country' column in your question.
please refer to this post. hope u can help :) stackoverflow.com/q/61335767/13276281
I had post my answer, pls accept if it works~ I tested: )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.