2016-11-19 114 views
1

當文件夾中存在文件時,出現文件不存在的錯誤,請問我在哪裏犯了錯誤?熊貓無法聚合

pd.DataFrame.from_csv 

我收到如下所示的錯誤。

Traceback (most recent call last): 
    File "main.py", line 194, in <module> 
    start_path+end_res) 
    File "/Users/admin/Desktop/script/mergeT.py", line 5, in merge 
    df_peak = pd.DataFrame.from_csv(peak_score, index_col = False, sep='\t') 
    File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1231, in from_csv 
    infer_datetime_format=infer_datetime_format) 
    File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 645, in parser_f 
    return _read(filepath_or_buffer, kwds) 
    File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 388, in _read 
    parser = TextFileReader(filepath_or_buffer, **kwds) 
    File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 729, in __init__ 
    self._make_engine(self.engine) 
    File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 922, in _make_engine 
    self._engine = CParserWrapper(self.f, **self.options) 
    File "/Library/Python/2.7/site-packages/pandas/io/parsers.py", line 1389, in __init__ 
    self._reader = _parser.TextReader(src, **kwds) 
    File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4175) 
    File "pandas/parser.pyx", line 667, in pandas.parse**strong text**r.TextReader._setup_parser_source (pandas/parser.c:8440) 
IOError: File results\scoring\fed\score_peak.txt does not exist 

我已經嘗試的路徑設置爲確切的文件 例如

+0

第一個錯誤:腳本的縮進錯誤。然後:backslaches而不是斜槓。可能您使用的是過時的文件,可能是在Windows中配置的。 – user3159253

+0

像@ user3159253表示你需要在函數'def'後面縮進所有內容,否則文件路徑不會被改變。 –

+0

@ user3159253您的意思是我導入數據,如peak_score ='\ Users \ admin \ Desktop \ peak_score.txt'?是的,它是基於Windows,現在我使用Mac –

回答

2

documentation of pandas 0.19.1pandas.DataFrame.from_csv不支持index_col = False。嘗試使用pandas.read_csv代替(使用相同的參數)。還要確保你使用的是熊貓的最新版本。

見,如果這個工程:

import pandas as pd 
def merge(peak_score, profile_score, res_file): 
    df_peak = pd.read_csv(peak_score, index_col = False, sep='\t') 
    df_profile = pd.read_csv(profile_score, index_col = False, sep='\t') 
    result = pd.concat([df_peak, df_profile], axis=1) 
    print result.head() 
    test = [] 
    for a,b in zip(result['prot_a_p'],result['prot_b_p']): 
     if a == b: 
      test.append(1) 
     else: 
      test.append(0) 
    result['test']=test 
    result = result[result['test']==0] 
    del result['test'] 
    result = result.fillna(0) 
    result.to_csv(res_file) 

if __name__ == '__main__': 
    pass 

關於改變從Windows到OS X時的路徑問題:

在Unix中的所有口味,路徑都寫有斜線/,而在Windows反斜槓\被使用。由於OS X是Unix的後裔,正如其他用戶已經正確指出的那樣,當您從Windows更改時,您需要調整路徑。

+0

輝煌!謝謝 –