2014-03-26 81 views
1

我編寫了一個Python腳本(如下),它從文本文件(使用pandas)加載數據並檢查列中的值。關鍵錯誤&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` 

回答

0

您加載文件到imageimage_tagged,而a處於閒置狀態。

我不知道data.Angle_numbernumrows是什麼,但它們似乎來自庫,與您的文件無關。

+0

嗨洛倫佐,我簡化了我的代碼太多了......我在上面的更新版本中從讀取txt獲取數據(請參閱if __name__ =='__main __':''下面的前兩行)。 –