pandas 将地理数据框与数据框合并时返回NAN值

x4shl7ld  于 2023-04-10  发布在  其他
关注(0)|答案(1)|浏览(130)

我读入的数据如下

polygons = polygons.geojson
polygons = gpd.read_file(polygons)

income = income.csv
provi_income = pd.read_csv(income)
income

    Primary_IncomeNet    identificatie
1               14734    PV20      
2               16502    PV21      
3               12917    PV22      
4               31189    PV23      
5               12060    PV24      
6               59168    PV25      
7               45021    PV26      
8               94199    PV27      
9              111357    PV28      
10              10405    PV29      
11              76537    PV30      
12              29362    PV31
polygons

  identificatie                                           geometry
0           PV20  MULTIPOLYGON (((265275.541 549247.459, 265285....
1           PV22  MULTIPOLYGON (((231437.815 516445.643, 231430....
2           PV26  MULTIPOLYGON (((131894.470 429932.357, 131917....
3           PV24  MULTIPOLYGON (((157694.139 473920.680, 159406....
4           PV23  MULTIPOLYGON (((248291.900 459808.449, 248302....
5           PV27  MULTIPOLYGON (((131700.944 464257.265, 131702....
6           PV25  MULTIPOLYGON (((181361.527 418255.386, 181384....
7           PV28  MULTIPOLYGON (((88397.000 413853.999, 89142.01...
8           PV21  MULTIPOLYGON (((189491.268 535832.617, 189494....
9           PV30  MULTIPOLYGON (((167891.450 359190.720, 168085....
10          PV31  MULTIPOLYGON (((199549.696 308385.049, 199558....
11          PV29  MULTIPOLYGON (((50235.786 357928.267, 50243.18...

然后根据公共id合并。这将合并两个数据框,但Primary_IncomeNet列显示NaN。为什么Primary_IncomeNet列不能正确合并?如何正确执行合并?

merge = polygons.merge(income, on="identificatie", how= 'left')
identificatie                                           geometry   Primary_IncomeNet
0           PV20  MULTIPOLYGON (((265275.541 549247.459, 265285....  NaN
1           PV22  MULTIPOLYGON (((231437.815 516445.643, 231430....  NaN
2           PV26  MULTIPOLYGON (((131894.470 429932.357, 131917....  NaN
3           PV24  MULTIPOLYGON (((157694.139 473920.680, 159406....  NaN
4           PV23  MULTIPOLYGON (((248291.900 459808.449, 248302....  NaN
5           PV27  MULTIPOLYGON (((131700.944 464257.265, 131702....  NaN
6           PV25  MULTIPOLYGON (((181361.527 418255.386, 181384....  NaN
7           PV28  MULTIPOLYGON (((88397.000 413853.999, 89142.01...  NaN
8           PV21  MULTIPOLYGON (((189491.268 535832.617, 189494....  NaN
9           PV30  MULTIPOLYGON (((167891.450 359190.720, 168085....  NaN
10          PV31  MULTIPOLYGON (((199549.696 308385.049, 199558....  NaN
11          PV29  MULTIPOLYGON (((50235.786 357928.267, 50243.18...  NaN
dffbzjpn

dffbzjpn1#

根据 Dataframe income的列(identificatie)的对齐方式,我怀疑她持有尾随的空格,因为它们的值是左对齐的。所以,在合并之前尝试strip它们。

income["identificatie"] = income["identificatie"].str.strip()
​
merge = polygons.merge(income, on="identificatie", how= "left")

输出:

print(merge)

   identificatie                                           geometry  Primary_IncomeNet
0           PV20  MULTIPOLYGON (((265275.541 549247.459, 265285....              14734
1           PV22  MULTIPOLYGON (((231437.815 516445.643, 231430....              12917
2           PV26  MULTIPOLYGON (((131894.470 429932.357, 131917....              45021
3           PV24  MULTIPOLYGON (((157694.139 473920.680, 159406....              12060
4           PV23  MULTIPOLYGON (((248291.900 459808.449, 248302....              31189
5           PV27  MULTIPOLYGON (((131700.944 464257.265, 131702....              94199
6           PV25  MULTIPOLYGON (((181361.527 418255.386, 181384....              59168
7           PV28  MULTIPOLYGON (((88397.000 413853.999, 89142.01...             111357
8           PV21  MULTIPOLYGON (((189491.268 535832.617, 189494....              16502
9           PV30  MULTIPOLYGON (((167891.450 359190.720, 168085....              76537
10          PV31  MULTIPOLYGON (((199549.696 308385.049, 199558....              29362
11          PV29  MULTIPOLYGON (((50235.786 357928.267, 50243.18...              10405

相关问题