我想使用GeoPandas来可视化一些选举数据。我有两个DataFrames -第一个DataFrame包含几何数据和分区标签,第二个DataFrame包含投票数据。我想将第二个DataFrame中的一些投票数据添加到第一个DataFrame中。
下面是第一个DataFrame的结构:
Precinct_2020 geometry
345 Precinct 4-8 POLYGON ((-95.93331 41.22970, -95.93330 41.230...
346 Precinct 4-9 POLYGON ((-95.95904 41.23577, -95.95889 41.235...
347 Precinct 4-3 POLYGON ((-95.94178 41.20966, -95.94178 41.211...
348 Precinct 2-17 POLYGON ((-95.95277 41.26891, -95.95255 41.270...
349 Precinct 8-83 POLYGON ((-96.04293 41.33597, -96.04294 41.337...下面是第二个DataFrame的结构:
Precinct_2020 diff
0 Precinct 1-2 67
1 Precinct 1-3 67
2 Precinct 1-4 27
3 Precinct 1-5 63
4 Precinct 1-7 43我尝试通过将分区标签与两个嵌套的for循环进行匹配来做到这一点,如下所示:
for entry in douglas_county_df:
for item in voting_diff:
if item['Precinct_2020'] in entry['Precinct_2020']:
entry['diff'] = item['diff']本质上,我希望将第二个DataFrame中的投票差值'diff‘添加到第一个DataFrame中的相应区域。我得到一个错误,字符串索引必须是整数。处理这个问题的最好方法是什么?
预期输出:
Precinct_2020 geometry
345 Precinct 4-8 POLYGON ((-95.93331 41.22970, -95.93330 41.230... [diff for 4-8]
346 Precinct 4-9 POLYGON ((-95.95904 41.23577, -95.95889 41.235... [diff for 4-9]
347 Precinct 4-3 POLYGON ((-95.94178 41.20966, -95.94178 41.211... [diff for 4-3]
348 Precinct 2-17 POLYGON ((-95.95277 41.26891, -95.95255 41.270... [diff for 2-17]
349 Precinct 8-83 POLYGON ((-96.04293 41.33597, -96.04294 41.337... [diff for 8-83]谢谢!
发布于 2020-11-09 11:48:34
解决方案
如果您有两个要合并的数据帧,请使用(就像David Erickson在注释中提到的那样):
COLUMN_TO_MERGE_ON = "Precinct_2020"
pandas.merge(df_left, df_right, how="left", on=COLUMN_TO_MERGE_ON)注意:
left join。因此,how = left.df_left和df_right就是您的数据帧。如果您只需要df_right数据帧中的几列,可以使用df_right[list_of_target_columns]作为df_right.参考文献:
我鼓励您探索以下参考资料。
https://stackoverflow.com/questions/64745380
复制相似问题