本文介绍了如何将县名转换为FIPS代码? (将县名映射到他们的fip)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张表,其中一列是县名,另一列是各种属性.
I have a table where one column are the county names and the other columns are various attributes.
我想将这一列的县名转换为fips代码.
I want to convert this column of county names to fips codes.
我有一个中间表,其中显示了每个县的fips代码.
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]
}
推荐答案
您可以使用合并".
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)
,输出为您的final_df.
and the output is your final_df.
这篇关于如何将县名转换为FIPS代码? (将县名映射到他们的fip)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!