使用 .merge GeoPandas 时清空 GeoDataFrame

Empty GeoDataFrame when using .merge GeoPandas

我正在尝试使用属性连接合并 pandas 数据框和 geopandas 地理数据框。我正在使用美国县形状文件(https://geonet.esri.com/thread/24614 first response) and a csv file (http://water.usgs.gov/watuse/data/2010/index.html 第一个 Excel 格式,然后另存为 csv)。使用 FIPS 连接数据框后,我尝试打印新的地理数据框,但仅打印 headers 并显示上面的消息 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 不应该合并来自 objects 的数据吗?我想制作一张地表淡水抽取的 Choropleth 地图。

抱歉,如果我们不打算列出我们使用的数据,我想尽可能具体。我是 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")