[英]Python - error in pandas (during loop processing files)
嗨,我在處理時遇到了奇怪的錯誤(從 CSV 文件中提取數據)我無法理解。 我是初學者,我編寫了一些代碼來提取 mri 生成的發作時間和持續時間。 我的代碼(如下)貫穿所有(144)個 csv 文件作為輸入文件,提取我需要的信息並將其保存為 output 目錄中的 csv 。 問題是處理 10 個文件后出現錯誤。 我真的不明白發生了什么,因為所有 csv 輸入文件的格式都相同。 你能幫我理解錯誤以及如何解決它嗎? 代碼在單個文件上正常工作,提取行的唯一問題是循環遍歷多個文件時。 任何幫助,將不勝感激!
import csv
import os
import pandas as pd
import math
import numpy as np
import csv
import glob
IN="/Users/*****/Documents/LMG_Env/RSA/RSA_mri/events/behavioral_stimuli_computer/CSVlog/" # points to csv files
OUT= "/Users/*****/Documents/LMG_Env/RSA/RSA_mri/events/onsets/" # outputted.
file_list = os.path.join(IN, '*.csv')
for infile in glob.glob(file_list):
# open log file and load it into table from 3rd row.
in_txt = pd.read_csv(infile, names=['Subject','Trial','Event Type','Code','Time','TTime','Uncertainty','Duration','Uncertainty_1','ReqTime','ReqDur','Stim Type','Pair Index'], skiprows=3, delimiter=',')
df = pd.DataFrame(in_txt,columns=['Subject','Trial','Event Type','Code','Time','TTime','Uncertainty','Duration','Uncertainty_1','ReqTime','ReqDur','Stim Type','Pair Index'])
# for each row in table extract only columns: Trial, Event type, Code, Time, Duration
columns = df.columns
extract_column = df[['Trial','Event Type', 'Code', 'Time','Duration']]
extract_column = extract_column.set_index('Event Type') #changing index to Event Type
# extract only rows with Event Type: Picture.
extract_rows = extract_column.loc['Picture']
#recalculare time & duration into seconds.
extract_rows.loc[:,'Time'] = extract_rows['Time'].div(10000) #to seconds
extract_rows.loc[:,'Duration'] = extract_rows['Duration'].div(10000) #to seconds
# save file in OUT as tsv (tab separated vectors)
filename = 'sub-' + os.path.splitext( os.path.basename(infile))[0] + '_events' + '.csv'
filename = os.path.join(OUT, filename)
extract_rows.to_csv(filename, index=False)
print(filename)
和錯誤:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
//anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2656 try:
-> 2657 return self._engine.get_loc(key)
2658 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Picture'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-15-640c70c49427> in <module>
11
12 # extract only rows with Event Type: Picture.
---> 13 extract_rows = extract_column.loc['Picture']
14
15 #recalculare time & duration into seconds.
//anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in __getitem__(self, key)
1498
1499 maybe_callable = com.apply_if_callable(key, self.obj)
-> 1500 return self._getitem_axis(maybe_callable, axis=axis)
1501
1502 def _is_scalar_access(self, key):
//anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
1911 # fall thru to straight lookup
1912 self._validate_key(key, axis)
-> 1913 return self._get_label(key, axis=axis)
1914
1915
//anaconda3/lib/python3.7/site-packages/pandas/core/indexing.py in _get_label(self, label, axis)
139 raise IndexingError('no slices here, handle elsewhere')
140
--> 141 return self.obj._xs(label, axis=axis)
142
143 def _get_loc(self, key, axis=None):
//anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in xs(self, key, axis, level, drop_level)
3583 drop_level=drop_level)
3584 else:
-> 3585 loc = self.index.get_loc(key)
3586
3587 if isinstance(loc, np.ndarray):
//anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2657 return self._engine.get_loc(key)
2658 except KeyError:
-> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'Picture'
df = pd.DataFrame(in_txt,columns=['Subject','Trial','Event Type','Code','Time','TTime','Uncertainty','Duration','Uncertainty_1','ReqTime','ReqDur','Stim Type','Pair Index'])
dataframe 沒有名為Picture
的列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.