Python 关键错误,熊猫在做奇怪的事情

Python 关键错误,熊猫在做奇怪的事情,python,pandas,keyerror,Python,Pandas,Keyerror,关于这个错误,我已经找了好几天了,没有任何改进。熊猫似乎在重复数据帧,然后再次重复。然而,当在特定的迭代中迭代键时,似乎会引发KeyError。这一切都可能是解释器的问题,还是我的代码中有错误? 任何帮助都将不胜感激 更多上下文: def extract_features(id_arr): features_df = pd.read_csv(r'D:\fma_metadata\features.csv', index_col=0, na_values=['NA'], encoding='utf-

关于这个错误,我已经找了好几天了,没有任何改进。熊猫似乎在重复数据帧,然后再次重复。然而,当在特定的迭代中迭代键时,似乎会引发KeyError。这一切都可能是解释器的问题,还是我的代码中有错误? 任何帮助都将不胜感激

更多上下文:

def extract_features(id_arr):
features_df = pd.read_csv(r'D:\fma_metadata\features.csv', index_col=0, na_values=['NA'], encoding='utf-8')
features = np.array(features_df.columns)
id_arr = np.asarray(id_arr, dtype=int)

for id in id_arr:
    row_features = []

    for key, value in features_df.iteritems():
        number = float(features_df[key][id])
        row_features.append(round(number, 6))

    row_features = np.asarray(row_features)
    features = np.vstack((features, row_features))

features = np.delete(features, 0, 0)

return features


random_id = get_random_id()

extract_features(random_id)
  Traceback (most recent call last):
  File "C:/Users/*****/PycharmProjects/****/emotions-nn/deep-learning/input.py", line 65, in <module>
    print(extract_features(random_id))
  File "C:/Users/*****/PycharmProjects/****/emotions-nn/deep-learning/input.py", line 51, in extract_features
    number = float(features_df[key][id])
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
    return self._get_value(key)
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\series.py", line 991, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\indexes\base.py", line 2891, in get_loc
    raise KeyError(key) from err
KeyError: 800
  • 特征子集_df:

  • 提供给函数的参数:np.ID数组(
    dtype=int
    ),将通过数据集进行搜索

这里有代码:

def extract_features(id_arr):
features_df = pd.read_csv(r'D:\fma_metadata\features.csv', index_col=0, na_values=['NA'], encoding='utf-8')
features = np.array(features_df.columns)
id_arr = np.asarray(id_arr, dtype=int)

for id in id_arr:
    row_features = []

    for key, value in features_df.iteritems():
        number = float(features_df[key][id])
        row_features.append(round(number, 6))

    row_features = np.asarray(row_features)
    features = np.vstack((features, row_features))

features = np.delete(features, 0, 0)

return features


random_id = get_random_id()

extract_features(random_id)
  Traceback (most recent call last):
  File "C:/Users/*****/PycharmProjects/****/emotions-nn/deep-learning/input.py", line 65, in <module>
    print(extract_features(random_id))
  File "C:/Users/*****/PycharmProjects/****/emotions-nn/deep-learning/input.py", line 51, in extract_features
    number = float(features_df[key][id])
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
    return self._get_value(key)
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\series.py", line 991, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\indexes\base.py", line 2891, in get_loc
    raise KeyError(key) from err
KeyError: 800
错误:

def extract_features(id_arr):
features_df = pd.read_csv(r'D:\fma_metadata\features.csv', index_col=0, na_values=['NA'], encoding='utf-8')
features = np.array(features_df.columns)
id_arr = np.asarray(id_arr, dtype=int)

for id in id_arr:
    row_features = []

    for key, value in features_df.iteritems():
        number = float(features_df[key][id])
        row_features.append(round(number, 6))

    row_features = np.asarray(row_features)
    features = np.vstack((features, row_features))

features = np.delete(features, 0, 0)

return features


random_id = get_random_id()

extract_features(random_id)
  Traceback (most recent call last):
  File "C:/Users/*****/PycharmProjects/****/emotions-nn/deep-learning/input.py", line 65, in <module>
    print(extract_features(random_id))
  File "C:/Users/*****/PycharmProjects/****/emotions-nn/deep-learning/input.py", line 51, in extract_features
    number = float(features_df[key][id])
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\series.py", line 882, in __getitem__
    return self._get_value(key)
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\series.py", line 991, in _get_value
    loc = self.index.get_loc(label)
  File "C:\Users\*****\anaconda3\envs\tensorflow\lib\site-packages\pandas\core\indexes\base.py", line 2891, in get_loc
    raise KeyError(key) from err
KeyError: 800
回溯(最近一次呼叫最后一次):
文件“C:/Users/******/PycharmProjects/******/emotions nn/deep learning/input.py”,第65行,在
打印(提取特征(随机id))
文件“C:/Users/*****/PycharmProjects/*****/emotions nn/deep learning/input.py”,第51行,在extract_features中
数字=浮动(特征为[key][id])
文件“C:\Users\*****\anaconda3\envs\tensorflow\lib\site packages\pandas\core\series.py”,第882行,在u getitem中__
返回自我。获取值(键)
文件“C:\Users\*****\anaconda3\envs\tensorflow\lib\site packages\pandas\core\series.py”,第991行,在"获取"值中
loc=自索引获取位置(标签)
文件“C:\Users\*****\anaconda3\envs\tensorflow\lib\site packages\pandas\core\index\base.py”,第2891行,在get_loc中
从err中升起钥匙错误(钥匙)
关键字错误:800

我猜这可能是您的多级索引

#id也可以是整数列表
def提取(ID:np.ndarray):
#假设前3行是“标题”
df=pd.read\u csv(r“C:\Users\danie\Downloads\features-subset.csv”,标题=[0,1,2],索引列=0,不适用值=['na'])
#可以在此处设置断点以查看当前列顺序
#打印(df.列)
#按照你想要的方式重新组织
#如果我没弄错的话,这就是你想要做的
将df.loc[ids].round(6)返回给_numpy()
#如果有列顺序
将df.loc[ids,order].round(6)返回给_numpy()

您能否发布.csv文件的子集和随机id参数的示例,我无法用当前示例codeUpdated@Joseph重现错误,也谢谢!非常感谢@dbokers。它确实有用!我也不知道df.loc,所以我也不知道