EXPLANATION: Basically I would like a post code range (6000 - 6997), to determine State "WA" and be listed as "WA" in the State column. So if the post code, for example, is 6100 - the State will be "WA".
There are 4 columns: Customer | Address | State | PostCode
Customer | Address | State | PostCode
Google | 123 Blah Street, Perth | null | 6100
Trying to determine 'State' based on a Post Code range: (6000,6997), should enter "WA" in the column 'State'. Tried several attempts but running into issues.
import re
find_postcode = re.compile(re.escape(x.startswith('6') == 4) df['State']= df.State.apply(lambda x: find_postcode.sub('WA', str(x)))
OR
PostCode = [range(6000,6997)] find_postcode = re.compile(r'\b(' + '|'.join(PostCode) + r')\b') df.State.apply(lambda x: find_postcode.sub('WA', str(x)) if x in PostCode else x)
Any guidance is much appreciated!