【问题标题】:Key error & Pandas关键错误和熊猫
【发布时间】:2014-03-26 15:28:16
【问题描述】:

我编写了一个 Python 脚本(如下),它从文本文件(使用 pandas)加载数据并检查列中的值。

import sys
import pandas as pd
import numpy as np
from numpy import ndarray
import math
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from skimage import data
from skimage.feature import match_template

if __name__ == '__main__':
    data = pd.read_csv('Fe_PSI_spt_refined.txt', sep=" ", header = None)
    data.columns = ["Angle_number", "Omega", "Intensity", "X", "Y", "Address", "ID"]#, "flag"]
    Number_of_projections = 181
    Number_of_lines_in_txt = 3493
    numrows = len(data)
    counter_array = []
    correlation_threshold_value = 0.7
    a = np.zeros(Number_of_lines_in_txt)
    output_file = ("output.txt")

    for i in range(2, (Number_of_projections + 1)): 
        filename_cutouts_combined = ("cutouts_combined_%03i.txt" % (i))
        filename_cutouts_combined_tag = ("cutouts_combined_tag_%03i.txt" % (i))
        image = np.loadtxt(filename_cutouts_combined)
        image_tagged = np.loadtxt(filename_cutouts_combined_tag)
        for j in range(0, Number_of_lines_in_txt - 1):
            print data.Angle_number[j], i

j 的一次迭代之后,我收到以下错误。您发现我应该修复的任何错误吗?谢谢

`Traceback (most recent call last):
  File "Hyperbola_search.py", line 46, in <module>
    print data.Angle_number[j], i
  File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 491, in __getitem__
    result = self.index.get_value(self, key)
  File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1032, in get_value
    return self._engine.get_value(s, k)
  File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2661)
  File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2476)
  File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3215)
  File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6450)
  File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6394)
KeyError: 3491`

【问题讨论】:

    标签: python matplotlib pandas keyerror


    【解决方案1】:

    您将文件加载到imageimage_tagged,而a 仍未使用。

    我不知道data.Angle_numbernumrows 是什么,但它们似乎来自库,与您的文件无关。

    【讨论】:

    • 嗨 Lorenzo,我的代码过于简化了...我通过在上面的更新版本中读取 txt(参见下面的前两行 if __name__ == '__main__':)来获取数据。
    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 2013-05-15
    • 2017-05-23
    • 2023-03-25
    相关资源
    最近更新 更多