首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KeyError,熊猫在做奇怪的事情

KeyError,熊猫在做奇怪的事情
EN

Stack Overflow用户
提问于 2020-09-21 11:45:32
回答 1查看 675关注 0票数 1

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

更多上下文:

https://www.transfernow.net/3yS7pE092020

  • 子集

传递给函数的

  • 参数:将通过dataset

搜索的ID的np.array (dtype=int)

这里有代码:

代码语言:javascript
复制
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)

错误:

代码语言:javascript
复制
  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
EN

回答 1

Stack Overflow用户

发布于 2020-09-21 13:28:16

我猜这可能是你的多层次索引。

代码语言:javascript
复制
# ids can be a list of integers too
def extract(ids: np.ndarray):
    # assuming the first 3 rows are "headers"
    df = pd.read_csv(r"C:\Users\danie\Downloads\features - subset.csv", header=[0,1,2], index_col=0, na_values=['NA'])

    # you can set a breakpoint here to see the current column order
    # print(df.columns)
    # and reorganize the way you want it

    # this is basically what you're trying to do if I'm not mistaken
    return df.loc[ids].round(6).to_numpy()

    # if there's a column order
    return df.loc[ids, order].round(6).to_numpy()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63991611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档