2016-07-30 58 views
0

任何幫助請從URL網站閱讀此文件。從UCL網站使用熊貓閱讀波士頓數據時出錯

eurl = 'https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data' 
data = pandas.read_csv(url, sep=',', header = None) 

我試過sep =',',sep =';'和SEP = '\ t',但這樣寫的 enter image description here

data = pandas.read_csv(url, sep=' ', header = None) 

我收到了錯誤的數據,

pandas/parser.pyx in pandas.parser.TextReader.read (pandas/parser.c:7988)() 
pandas/parser.pyx in pandas.parser.TextReader._read_low_memory (pandas/parser.c:8244)() 
pandas/parser.pyx in pandas.parser.TextReader._read_rows (pandas/parser.c:8970)() 
pandas/parser.pyx in pandas.parser.TextReader._tokenize_rows (pandas/parser.c:8838)() 
pandas/parser.pyx in pandas.parser.raise_parser_error (pandas/parser.c:22649)() 
CParserError: Error tokenizing data. C error: Expected 30 fields in line 2, saw 31 

也許同樣的問題在這裏問enter link description here但接受的答案不幫助我。

任何幫助請從URL中讀取此文件提供它。

順便說一句,我知道有Boston = load_boston()來讀取這個數據,但是當我從這個函數讀取它時,數​​據集中的屬性'MEDV'不會與數據集一起下載。

回答

1

有用作分隔符多個空格,這就是爲什麼當你使用一個空格作爲分隔符(sep=' '

你可以做到這一點使用sep='\s+'不工作:

In [171]: data = pd.read_csv(url, sep='\s+', header = None) 

In [172]: data.shape 
Out[172]: (506, 14) 

In [173]: data.head() 
Out[173]: 
     0  1  2 3  4  5  6  7 8  9  10  11 12 13 
0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296.0 15.3 396.90 4.98 24.0 
1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242.0 17.8 396.90 9.14 21.6 
2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242.0 17.8 392.83 4.03 34.7 
3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222.0 18.7 394.63 2.94 33.4 
4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222.0 18.7 396.90 5.33 36.2 

,或者使用delim_whitespace=True

In [174]: data = pd.read_csv(url, delim_whitespace=True, header = None) 

In [175]: data.shape 
Out[175]: (506, 14) 

In [176]: data.head() 
Out[176]: 
     0  1  2 3  4  5  6  7 8  9  10  11 12 13 
0 0.00632 18.0 2.31 0 0.538 6.575 65.2 4.0900 1 296.0 15.3 396.90 4.98 24.0 
1 0.02731 0.0 7.07 0 0.469 6.421 78.9 4.9671 2 242.0 17.8 396.90 9.14 21.6 
2 0.02729 0.0 7.07 0 0.469 7.185 61.1 4.9671 2 242.0 17.8 392.83 4.03 34.7 
3 0.03237 0.0 2.18 0 0.458 6.998 45.8 6.0622 3 222.0 18.7 394.63 2.94 33.4 
4 0.06905 0.0 2.18 0 0.458 7.147 54.2 6.0622 3 222.0 18.7 396.90 5.33 36.2 
+0

很多人感謝你的工作 –