函数仅返回单个DataFrame,无法访问多个DataFrames问题排查
问题分析与修复方案
你的问题出在函数的循环逻辑上:
- 循环遍历
in_features中的每个要素类时,每次生成的sdf都会覆盖前一次的结果,函数最后返回的只是最后一个要素类转换得到的DataFrame,并不是包含所有结果的列表。所以你调用函数后得到的是单个DataFrame,用[1]索引自然会报键错误。
修复方法很简单,只需要创建一个列表来收集每个转换后的DataFrame:
import pandas as pd from arcgis.features import GeoAccessor, GeoSeriesAccessor CHList=arcpy.ListFeatureClasses('*CH') # list feature classes from ArcGIS Pro and assign to variable def DF_from_Ftr(in_features): # 创建空列表存储所有DataFrame df_list = [] for feature in in_features: sdf = pd.DataFrame.spatial.from_featureclass(feature) # 用GeoAccessor将要素类转为DataFrame sdf.drop(['SHAPE'], axis=1, inplace=True) # 将当前转换好的DataFrame添加到列表 df_list.append(sdf) # 返回包含所有结果的列表 return df_list
现在调用DF_from_Ftr(CHList)会得到一个包含所有DataFrame的列表,你可以用[0]、[1]这类索引来访问对应的DataFrame了。
内容的提问来源于stack exchange,提问作者PCMitchell
相关产品推荐
相关产品推荐

