繁体   English   中英

使用.merge GeoPandas时为空的GeoDataFrame

[英]Empty GeoDataFrame when using .merge GeoPandas

我正在尝试通过使用属性联接来合并熊猫数据框和地理熊猫地理数据框。 我正在使用美国各州的形状文件( https://geonet.esri.com/thread/24614第一个响应)和一个csv文件( http://water.usgs.gov/watuse/data/2010/index.html第一种Excel格式,然后另存为csv)。 使用FIPS将数据框加入后,我将尝试打印新的地理数据框,但是只有标头会显示上述消息Empty GeoDataFrame。 这是我正在使用的代码

from matplotlib import pyplot as plt
import csv 
import numpy as np
import datetime
import pandas as pd
import geopandas as gpd
import csv
from shapefile import Reader 

df1 = pd.read_csv('usco2010.csv') 
#Reads csv file and puts it into a dataframe
df2 = pd.DataFrame({'STATE':df1['STATE'],'COUNTY':df1['COUNTY'],'FIPS':df1['FIPS'],'Se    rPop10^3':df1['TP-TotPop'],'WtrWthdrwl_MGD':df1['PS-WSWFr']})  
#Takes the data we want from df1 and creates a new dataframe df2: Statename, County name, FIPS, Served Population in 1000s, Surface water withdrawls in MGD 


counties = gpd.read_file('UScounties') 
#creates a GeoDataFrame for the US counties by using UScounties shapefile

print(counties.head())
print(df2.head())

counties = counties.merge(df2, on='FIPS') #Empty GeoDataFrame
#merges counties and df2 with same FIPS

print(counties)

GeoDataFrame是否不应该合并两个对象的数据? 我想制作一个Choropleth Maps以提取新鲜的地表水。

抱歉,如果我们不打算列出我们使用的数据,我想尽可能具体。 我是python的新手,所以如果这是一个简单的问题,我深表歉意,但是google和此网站上的搜索未显示已回答的类似问题

刚刚检查了您的问题。 从我的角度来看,问题在于您尝试合并的列涉及两种不同数据类型的位置。 县数据集中的列属于类型对象(例如,它包含字符串),而df2 [“ FIPS”]的类型为“ int64”。 这就是我所做的,以及合并工作。 仍然检查一下合并是否正确:

us_data = pd.read_csv('usco2010.csv') 
counties = gp.read_file('UScounties.shp') 
counties["FIPS"] = counties["FIPS"].astype(int)
pd.merge(counties, us_data, on="FIPS")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM