首页 > 解决方案 > 在 Pandas 中获取 KeyError

问题描述

我正在尝试从不同的模块调用一个函数,如下所示:

module1 - func1: returns a dataframe

module1 - func2(p_df_in_fromfunc1)

功能2:

for i in range(0,len(p_df_in_fromfunc1):
    # Trying to retrieve row values of individual columns and assign to variables
    v_tmp = p_df_in_fromfunc1.loc[i,"Col1"]

尝试运行上述代码时,出现错误:

关键错误 0

问题可能是因为我没有零编号的行吗?

标签: pythonpandas

解决方案


在不了解您的代码的情况下,我的猜测是,对于位置索引,如果您对索引方式感兴趣,请尝试使用 iloc 而不是 loc。就像是:

v_tmp = p_df_in_fromfunc1.iloc[i,"Col1"]

推荐阅读