为什么从 pandas df 转换为字典时会丢失记录?

Why are there missing records when I convert from pandas df to dictionary?

我正在尝试将从美国县的 shapefile 创建的大约 3233 条记录的 DBF 转换为数据框;然后我想从该数据框中取出两列,然后 转换为字典,其中 column1 是键,column2 是值 。但是,生成的字典没有与我的数据框相同数量的记录。

代码:

county_shapefile = "U:/Shapefiles/tl_2018_us_county/tl_2018_us_county.shp"
dbf = arcpy.TableToTable_conversion(county_shapefile,"U:/","county_data.dbf")

from simpledbf import Dbf5
dbfile = Dbf5(str(dbf))
df = dbfile.to_dataframe()

df_dict = {row[0]:row[1] for row in df.values}

我也尝试过使用 .to_dict() 函数执行此操作,但我没有获得所需的字典结构 {column1:column2,column1:column2...}

from simpledbf import Dbf5
dbfile=Dbf5(str(dbf))
df=dbfile.to_dataframe()
subset=df[["STATEFP","COUNTYFP"]]
subset=subset.set_index("COUNTYFP")
dict=subset.to_dict()

最后,我希望创建一个字典,其中键是县 FIPS 代码 (COUNTYFP),值是州 FIPS 代码 (STATEFP)。我不想有任何嵌套字典,只是一个格式为...

的简单字典
dict={
   COUNTYFP1:STATEFP1,
   COUNTYFP2:STATEFP2,
   COUNTYFP3:STATEFP3,
   ....
}

您确定 column1 没有重复项吗?因为 python 中的字典不支持重复键!如果要将 column1 中的所有值保留为键,则必须找到 workaround for the same.