为什么从 pandas df 转换为字典时会丢失记录?
Why are there missing records when I convert from pandas df to dictionary?
我正在尝试将从美国县的 shapefile 创建的大约 3233 条记录的 DBF 转换为数据框;然后我想从该数据框中取出两列,然后 转换为字典,其中 column1
是键,column2
是值 。但是,生成的字典没有与我的数据框相同数量的记录。
- 我使用 arcpy 调用美国所有县的 shapefile。当我使用 arcpy.GetCount_management(county_shapefile) 时,这个 returns 特征数为 3233 条记录。
- 为了转换成dataframe,我先用arcpy.TableToTableconversion()转换成dbf,这个returns一个有3233条记录的dbf。
- 使用 simpledbf 的 Dbf5 转换为 df 后,我得到了一个包含 3233 条记录的 df。
- 然后我将前两列转换为 returns 56 条记录 的字典。谁能告诉我这是怎么回事? (我最近从 Python 2 切换到 Python 3,这可能是问题的一部分吗?)
代码:
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.
我正在尝试将从美国县的 shapefile 创建的大约 3233 条记录的 DBF 转换为数据框;然后我想从该数据框中取出两列,然后 转换为字典,其中 column1
是键,column2
是值 。但是,生成的字典没有与我的数据框相同数量的记录。
- 我使用 arcpy 调用美国所有县的 shapefile。当我使用 arcpy.GetCount_management(county_shapefile) 时,这个 returns 特征数为 3233 条记录。
- 为了转换成dataframe,我先用arcpy.TableToTableconversion()转换成dbf,这个returns一个有3233条记录的dbf。
- 使用 simpledbf 的 Dbf5 转换为 df 后,我得到了一个包含 3233 条记录的 df。
- 然后我将前两列转换为 returns 56 条记录 的字典。谁能告诉我这是怎么回事? (我最近从 Python 2 切换到 Python 3,这可能是问题的一部分吗?)
代码:
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.