Mike Johnson Jr
Mike Johnson Jr

Reputation: 796

Use pandas to get county name using fips codes

I have fips codes here: http://www2.census.gov/geo/docs/reference/codes/files/national_county.txt

And a dataset that looks like this:

fips_state    fips_county       value

1             1                 10
1             3                 34
1             5                 37
1             7                 88
1             9                 93

How can I get the county name of each row using the data from the link above with pandas?

Upvotes: 2

Views: 2467

Answers (1)

John Zwinck
John Zwinck

Reputation: 249562

Simply load both data sets into DataFrames, then set the appropriate index:

df1.set_index(['fips_state', 'fips_county'], inplace=True)

This gives you a MultiIndex by state+county. Once you've done this for both datasets, you can trivially map them, for example:

df1['county_name'] = df2.county_name

Upvotes: 4

Related Questions