You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

函数仅返回单个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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 03:01:20