Clay Chester
Clay Chester

Reputation: 91

How do I convert county names to fips codes? (map county names to their fips)

I have a table where one column are the county names and the other columns are various attributes.

I want to convert this column of county names to fips codes.

I have an intermediary table that shows the fips code for each county.

Here is an example of what data i have (initial, and intermediate) and the data i want (final).

initial_df = {
    'county': ['REAGAN', 'UPTON', 'HARDEMAN', 'UPTON'], 
    'values': [508, 364, 26, 870]
}
intermediate_df = {
    'county': ['REAGAN', 'HARDEMAN', 'UPTON'], 
    'fips': [48383, 47069, 48461]
}
final_df = {
    'county': ['REAGAN', 'UPTON', 'HARDEMAN', 'UPTON'], 
    'fips': [48383, 48461, 47069, 48461], 
    'values': [508, 364, 26, 870]
}

Upvotes: 0

Views: 1423

Answers (3)

Alexander
Alexander

Reputation: 109510

You can take the dictionary from intermediate_df and convert it into a dictionary keyed on the county name with fips as the values. Then use this to map the county field in the initial_df.

mapping = {k: v for k, v in zip(*intermediate_df.values())}

df_final = pd.DataFrame(initial_df)
df_final['fips'] = df_final['county'].map(mapping)
>>> df_final
     county  values   fips
0    REAGAN     508  48383
1     UPTON     364  48461
2  HARDEMAN      26  47069
3     UPTON     870  48461

Upvotes: 1

Sree
Sree

Reputation: 983

You can use 'merge'.

import pandas as pd
initial_df = {'county': ['REAGAN', 'UPTON', 'HARDEMAN', 'UPTON'], 'values': [508, 
364, 26, 870]}
intermediate_df = {'county': ['REAGAN', 'HARDEMAN', 'UPTON'], 'fips': [48383, 47069, 
48461]}
final_df = {'county': ['REAGAN', 'UPTON', 'HARDEMAN', 'UPTON'], 'fips': [48383, 
48461, 47069, 48461], 'values': [508, 364, 26, 870]}
df1=pd.DataFrame(initial_df)
df2=pd.DataFrame(intermediate_df)
df3=df1.merge(df2)
print(df3)

and the output is your final_df.

Upvotes: 2

sacuL
sacuL

Reputation: 51335

Here is one way:

initial_df = pd.DataFrame(initial_df)

final_df = initial_df.assign(fips = initial_df['county'].map(dict(zip(*intermediate_df.values()))))

Or:

initial_df = pd.DataFrame(initial_df)

final_df = initial_df.assign(fips = initial_df['county'].map(pd.DataFrame(intermediate_df).set_index('county')['fips']))

Both result in:

>>> final_df
     county  values   fips
0    REAGAN     508  48383
1     UPTON     364  48461
2  HARDEMAN      26  47069
3     UPTON     870  48461

Upvotes: 1

Related Questions