簡體   English   中英

熊貓:read_csv,在看似常規數據上標記數據時出錯

[英]Pandas: read_csv, Error tokenizing data on seemingly regular data

我正在嘗試從以下網址讀取Elnino數據集: https : //archive.ics.uci.edu/ml/machine-learning-databases/el_nino-mld/el_nino.data.html

但是,我收到“錯誤標記數據”。 使用寫字板打開時,數據本身如下所示:

1 1   8.96 -140.32 -6.3  -6.4  83.5 27.32 27.57
1 2   8.95 -140.32 -5.7  -3.6  86.4 26.70 27.62
1 3   8.96 -140.32 -6.2  -5.8  83.0 27.36 27.68
1 4   8.96 -140.34 -6.4  -5.3  82.2 27.32 27.70
1 5   8.96 -140.33 -4.9  -6.2  87.3 27.09 27.85
1 6   8.96 -140.33 -6.3  -4.9  91.5 26.82 27.98
1 7   8.97 -140.32 -6.7  -3.7  94.1 26.62 28.04
1 8   8.96 -140.33 -6.3  -4.8  92.0 26.89 27.98 
1 9   8.97 -140.33 -6.3  -4.9  86.9 27.44 28.13
1 10  8.97 -140.32 -4.2  -2.5  87.3 26.62 28.14
1 11  8.96 -140.32 -6.8  -2.4  86.0 27.60 28.09
1 12  8.96 -140.33 -7.1  -3.2  82.2 27.87 28.15
1 13  8.96 -140.33 -6.7  -4.7  81.3 27.75 28.19

在我看來,這沒有問題。 到目前為止,我已經嘗試過:

pd.read_csv('elnino', sep=' |  |   |\t', header=None) # ValueError: Expected 13 fields in line 11, saw 35
pd.read_csv('elnino', sep=' ', error_bad_lines=False, header=None) # undesirable, because I'm losing more than half the lines, which are fine and the resulting dataframe still has a lot of nans

輸入數據有什么問題?

在閱讀了前幾行后,我注意到由sep=' | | |\\t' sep=' | | |\\t' sep=' | | |\\t' 這樣三個空格被解釋為“ [sep] nan [sep]”。

解決方案是:

df = pd.read_csv('elnino', sep=' *', header=None)

編輯 :剛注意到,這可能是一個更合適的解決方案:df = pd.read_csv('elnino',delim_whitespace = True,header = None)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM