4

I have a dataframe with full addresses in a column, and I need to create a separate column with just the zip code. Some of the addresses just have the five digit zip code whereas others have the additional four digits.

How do I split the column to just get the zip code?

Example Data

d = {'name':['bob','john'],'address':['123 6th Street,Sterling VA 20165-7513','567 7th Street, Wilmington NC 28411']}
df = pd.DataFrame(d)

I tried using rpartition but I get everything before the zip code:

df['test'] = df['address'].str.rpartition(" ")
print(df)
name    address                                test
bob     123 6th Street,Sterling VA 20165-7513  123 6th Street,Sterling VA
john    567 7th Street, Wilmington NC 28411    567 7th Street, Wilmington NC

This is what I'm trying to get:

name    address                                zipcode
bob     123 6th Street,Sterling VA 20165-7513  20165-7513
john    567 7th Street, Wilmington NC 28411    28411
Bob White
  • 733
  • 5
  • 20
Dread
  • 789
  • 3
  • 16
  • 31

3 Answers3

7

Use a regex with str.extract():

df['zip'] = df['address'].str.extract(r'(\d{5}\-?\d{0,4})')

returns:

   name                                address         zip
0   bob  123 6th Street,Sterling VA 20165-7513  20165-7513
1  john    567 7th Street, Wilmington NC 28411       28411

See the pandas page on str.extract() and the python page on re.

In particular, the {5} specifies that we must match 5 repetitions of \d (a numerical digit), while {0,4} indicates that we can match from 0 to 4 repetitions.

Brendan
  • 3,901
  • 15
  • 23
1

You can Try this

df['zip']= [i[-1] for i in df.address.str.split(' ').values]
Fouad Selmane
  • 378
  • 2
  • 11
0

You need to split the spaces, get the last item and you'll have the zipcode.

Something like this:

zipcodes = list()

for item in d['address']:
    zipcode = item.split()[-1]
    zipcodes.append(zipcode)

d['zipcodes'] = zipcodes
df = pd.DataFrame(d)