2017-04-24 29 views
0

例如,在數據的行看起來像這樣Python的熊貓閱讀形式標籤<index_1>的數據:<val_1><index_2>:<val_2>

-1 0:183.3575741549828 1:3.11164735151736 2:2.171277907851733 3:26.68849990272964 4:24.76677388937082 5:0.02710337995527495 

爲什麼指定索引的原因是因爲沒有指定屬性,爲指數假定爲零。

我試圖使用的語句:

train = pd.read_csv('train.csv', header=None, delim_whitespace=True).values 

它顯示了以下錯誤:

train = pd.read_csv('train.csv', header=None, delim_whitespace=True).values

File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 646, in parser_f return _read(filepath_or_buffer, kwds)

File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 401, in _read data = parser.read()

File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 939, in read ret = self._engine.read(nrows)

File "/usr/local/lib/python2.7/site-packages/pandas/io/parsers.py", line 1508, in read data = self._reader.read(nrows)

File "pandas/parser.pyx", line 848, in pandas.parser.TextReader.read (pandas/parser.c:10415)

File "pandas/parser.pyx", line 870, in pandas.parser.TextReader._read_low_memory (pandas/parser.c:10691)

File "pandas/parser.pyx", line 924, in pandas.parser.TextReader._read_rows (pandas/parser.c:11437)

File "pandas/parser.pyx", line 911, in pandas.parser.TextReader._tokenize_rows (pandas/parser.c:11308)

File "pandas/parser.pyx", line 2024, in pandas.parser.raise_parser_error (pandas/parser.c:27037)

pandas.io.common.CParserError: Error tokenizing data. C error: Expected 132 fields in line 5, saw 143

我似乎無法在這裏找出問題。任何幫助將是偉大的!

+0

能否請您編輯錯誤成可讀的格式?另外,我無法在數據中查看該行。那是一排字典嗎? – splinter

+0

@splinter 這是一個csv文件中的一行。屬性的數量是一個固定的數字,例如4125(0-4124)。一行指定訓練示例的屬性值,2:1231表示屬性2是1231 –

回答

0

根據您的數據描述和錯誤消息,我的猜測是您的csv文件中的行不具有每行相同數量的字段。嘗試指定字段列:

my_cols = range(0,4125) 
train = pd.read_csv('train.csv', header=None, delim_whitespace=True, names=my_cols).values 

可以找到更多的幫助:import csv with different number of columns per row using Pandas這裏:Handling Variable Number of Columns with Pandas - Python

+0

這是否解決了您的問題,還是需要進一步的幫助? – Nyps