2016-05-20 5 views
1

次のコードは、PandasにValueErrorを発生させます。私はなぜ正常なリストを使用して正常に動作するか分からない。Pandasが列名のリストを使用しようとするとValueErrorが発生する

fileFields = [str(input("Please enter the column name for the pedigree field in 
        your request file.\n")), 
       str(input("Please enter the column name for the pedigree field 
        in the Tissue Library file.\n")), 
       str(input("Please enter the column name for the sourceID field 
        in the Tissue Library file.\n")), 
       str(input("Please enter the column name for the pedigree field in 
        the Gold Standard file.\n")), 
       str(input("Please enter the column name for the sourceID field in 
        the Gold Standard file.\n"))] 

dfRequests = pd.read_csv(fileInputs[0], skipinitialspace=True, 
         usecols=fileFields[0]) 
dfTissueLibrary = pd.read_csv(fileInputs[1], skipinitialspace=True, 
           usecols=fileFields[1:2]) 
dfGoldStandard = pd.read_csv(fileInputs[2], skipinitialspace=True, 
          usecols=fileFields[3:4]) 

結果:

Traceback (most recent call last): 
    File "filepathway hidden for security", line 74, in <module> 
    usecols=fileFields[0]) 
    File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 529, in parser_f 
    return _read(filepath_or_buffer, kwds) 
    File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 295, in _read 
    parser = TextFileReader(filepath_or_buffer, **kwds) 
    File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 612, in __init__ 
    self._make_engine(self.engine) 
    File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 747, in _make_engine 
    self._engine = CParserWrapper(self.f, **self.options) 
    File "filepathway hidden for security\Local\Continuum\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1154, in __init__ 
    col_indices.append(self.names.index(u)) 
ValueError: 'd' is not in list 

パンダがfileFieldsリストの各インデックスから文字列を取得し、文字列のリストにそれらを回しているかのように私は感じています。私は、呼び出した後にインデックス付きの文字列リストを作成することでこれを解決しようとしましたが、うまくいきませんでした。助言がありますか?

+1

'fieldField [0]'は文字列(最初に入力されたカラム)を返します。したがって、 'd'はおそらく最初のカラムの最初の文字でしょうか?もしそうなら、 'usecols = fieldFields'を設定します。 – miraculixx

答えて

1

私のアプローチは、そのプロセスがシンプルで安全になり、次のように小さなヘルパー関数を使用することです:

def selective_read_csv(purpose, path): 
    # read just the header row and get the column names 
    columns = list(pd.read_csv(path, nrows=1).columns.values) 
    df = None 
    while df is None: 
     # present user with a selection of actual columns, taking 
     # out the guess work 
     file_fields = raw_input("[%s] Enter columns as a comma-separated list %s " % (purpose, columns)) 
     try: 
      df = pd.read_csv(path, usecols=file_fields.split(',')) 
     except ValueError as e: 
      print "Sorry, %s" % e 
      df = None 
    return df 
df = selective_read_csv('requests file', '/tmp/data.csv') 

ユーザーがファイルに実際にあると誤って入力された列が表示されます。この方法うまく扱わ:

[requests file] Enter columns as a comma-spearated list [u'a', u'b'] aaa 
Sorry, 'aaa' is not in list 
[requests file] Enter columns as a comma-spearated list [u'a', u'b'] 

次に例えば、各ファイルタイプのため、この関数を呼び出す:

dfRequests = selective_read_csv('requests file', fileInputs[0]) 
dfTissueLibrary = selective_read_csv('tissue library', fileInputs[1]) 
dfGoldStandard = selective_read_csv('gold standard', fileInputs[2]) 
関連する問題