Python Forum

Full Version: PowerBI: Using Python Regex to look for values in column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to add a column called ofInterest to select UK postcodes that match RG, OX & SA, the column would contain a 1 if the match is found, or 0 if not found.
My code is:

import pandas as pd
import re
dataset["ofInterest"] = dataset["RegAddress.PostCode"].re.search("[SRO][AGX][0-9]")
I'm sure I've got this all wrong because I have very little experience of Python (I know REGEX quite well) - can anybody help me?
(Oct-14-2022, 10:24 AM)MarcusR44 Wrote: [ -> ]re.search("[SRO][AGX][0-9]"
I hope that you are aware also SG, SX, RA, RX, OA and OG will be selected. I think you would rather use:
re.search(r"(RG|OX|SA)[0-9]"