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