为什么我在 pandas 中使用循环时会收到此 KeyErrores?

问题描述 投票:0回答:1

当我创建 3 个 keras 模型(针对 3 种不同类型的数据)时,我尝试在我的大数据集中使用这些模型/我使用“Rock”列来选择一个模型,然后我使用来自另一个 df 的数据(具有相同的数据集) lenght)来预测“Rv_synth”列的值。

df_cut

DEPT    RXOI    SSW TOPS    AT_90   RH39_1DF    RV39_1DF    Rock    GR  ZONES   Rv_synth
68652   3432.60 13.239096   19608.101563    10.0    12.784300   11.323830   29.956650   0.000000    121.453171  1   0
68653   3432.65 28.610432   19782.144531    10.0    14.439166   12.498925   31.454845   0.000000    114.461037  1   0
68654   3432.70 15.997140   19726.130859    10.0    16.094030   13.674020   32.953041   0.000000    107.468910  1   0
68655   3432.75 13.241823   19628.154297    10.0    18.447453   15.684806   35.002056   0.000000    100.766411  1   0
68656   3432.80 12.579830   20877.529297    10.0    20.800871   17.695589   37.051071   0.000000    94.063919   1   0


df_big_X

 RXOI   AT_90   GR
68652   13.239096   12.784300   121.453171
68653   28.610432   14.439166   114.461037
68654   15.997140   16.094030   107.468910
68655   13.241823   18.447453   100.766411
68656   12.579830   20.800871   94.063919
...

但是当我跑圈时它会向我显示这些 KeyErrors

for i in range(0, len(df_big_X)):
    if df_cut['Rock'][i].array == 1:
        y_pred = model_sand_n.predict(df_big_X[i:i+1])
        df_cut['Rv_synth'][i] = y_pred
    elif df_cut['Rock'][i] == 0:
        y_pred = model_shale_n.predict(df_big_X[i:i+1])
        df_cut['Rv_synth'][i] = y_pred
    elif df_cut['Rock'][i] == 6:
         y_pred = model_cal_n.predict(df_big_X[i:i+1])
         df_cut['Rv_synth'][i] = y_pred       
type here

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File c:\Users\makas\anaconda3\Lib\site-packages\pandas\core\indexes\base.py:3791, in Index.get_loc(self, key)
   3790 try:
-> 3791     return self._engine.get_loc(casted_key)
   3792 except KeyError as err:

File index.pyx:152, in pandas._libs.index.IndexEngine.get_loc()

File index.pyx:181, in pandas._libs.index.IndexEngine.get_loc()

File pandas\_libs\hashtable_class_helper.pxi:2606, in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas\_libs\hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 0

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
Cell In[60], line 2
      1 for i in range(0, len(df_big_X)):
----> 2     if df_cut['Rock'][i].array == 1:
      3         y_pred = model_sand_n.predict(df_big_X[i:i+1])
      4         df_cut['Rv_synth'][i] = y_pred
...
   3801     #  InvalidIndexError. Otherwise we fall through and re-raise
   3802     #  the TypeError.
   3803     self._check_indexing_error(key)

KeyError: 0
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

我尝试使用 df.iloc,但没有得到任何结果

pandas dataframe keras
1个回答
0
投票

当您尝试访问 DataFrame 中不存在的列、行或索引标签时,pandas 中的 KeyError 通常会发生。以下是一些常见原因和解决方案:

拼写错误或不匹配:仔细检查用于访问列/索引的名称是否正确,包括大写和任何特殊字符。

列未加载:如果您从 CSV 或其他文件加载数据,请确保该列确实存在。有时数据格式或标题可能会导致问题。

链式操作:当您使用 df[df["Column"] > 5]["NonexistentColumn"] 等链式索引时,pandas 可能会丢失对列的跟踪,从而导致 KeyError。尝试分解它或使用 .loc 代替。

列已删除或重命名:如果您之前删除或重命名了列,您将无法使用旧名称访问它。使用 df.columns 进行验证以查看当前的列名称。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.