0

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!

Jayne
  • 1
  • 1

1 Answers1

-2

Reading through this related SO Answer will help clarify some things and lead you in the right direction for future decisions. Also the conditional statement you might be looking for is

if PostCode >= 6000 and PostCode <= 6997
RiQts
  • 74
  • 3