user21006068
user21006068

Reputation:

Using pyzipcodes to create a States column from Zip Code column

I have a dataframe called df1. It includes a zip code column (ie., includes zip codes) and I want to use the pyzipcodes library to create a new column called state, and map the zip codes to states. Does anyone have an idea how to call on and use the pyzipcodes and achieve this task?

I was able to get entries in the state could that look like "ZipCode(zip='85711', city='Tucson', state='AZ'..." but I just want to extract the relevant state with the zip code.

Upvotes: -1

Views: 3138

Answers (2)

mikey
mikey

Reputation: 1210

Using this approach is better for handling errors, which can occur with large datasets.

Assuming you have a dataframe called df with a column called "zip_code":

from pyzipcode import ZipCodeDatabase

def get_state(zip_code):
    try:
        zcdb = ZipCodeDatabase()
        result = zcdb[zip_code]
        return result.state
    except:
        return None

df['state'] = df['zip_code'].apply(get_state)

If there is an error when finding the zip code, it will return a null value and proceed with the next row

Upvotes: 0

Timeless
Timeless

Reputation: 37847

You can use pandas.Series.map to map the column of zipcodes with the ZipCode object (that is basically a dictionnary) created by pyzipcode.

from pyzipcode import ZipCodeDatabase

zcdb = ZipCodeDatabase()

df["state"] = df["zp"].map(lambda x: zcdb[x].state)

Output :

print(df)

      zp state
0  85711    AZ

Upvotes: 2

Related Questions