Use pandas to get county name using fips codes

2.3k views Asked by At

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?

1

There are 1 answers

0
John Zwinck On BEST ANSWER

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