首页 > 解决方案 > Python Pandas:TypeError:只能将整数标量数组转换为标量索引

问题描述

我目前正在尝试将 shapefile 与 csv 合并。不幸的是,我的代码不起作用,因为我收到以下错误:

---> 17 merged = gdf.merge(df, left_on = 'ID', right_on = 'ID')
TypeError: only integer scalar arrays can be converted to a scalar index

我的代码如下:

shapefile ='C:/Users/User/Desktop/Shapefile/Stadtteil.shp'

gdf = gpd.read_file(shapefile)[['ID2', 'ID2FLOAT' , 'Shape_Leng' , 'Shape_Area']]

gdf.columns = [['ID','IDFLOAT','Shape_Leng' , 'Shape_Area']]

gdf.head()

import pandas as pd

datafile = 'C:/Users/User/Desktop/Stadtteile3.csv' 

df = pd.read_csv(datafile, names = ['ID','Einwohner'], skiprows = 1) 
df.columns = df.columns.get_level_values(0)
df.head()

merged = gdf.merge(df, left_on = 'ID', right_on = 'ID')


#---------------------------------------------------------
# I also tried the following:

#df['ID']=df['ID'].astype(int)
#gdf = gdf.astype(int)
#gdf['ID']=gdf['ID'].astype(int)

#import numpy as np
#x=np.concatenate(gdf['ID'],df['ID'])
#---------------------------------------------------------

数据文件如下:

Excel 截图 小水电截图

太感谢了!

标签: pythonpandasdataframetypeerror

解决方案


推荐阅读