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]
}
You can take the dictionary from
intermediate_df
and convert it into a dictionary keyed on the county name withfips
as the values. Then use this tomap
thecounty
field in theinitial_df
.You can use 'merge'.
and the output is your final_df.
Here is one way:
Or:
Both result in: