2017-07-30 40 views
0

我想直接從網站加載數據集到jypyter筆記本,但每次嘗試使用python熊貓的'read_csv'上載數據集時,都會導入數據集但我無法從數據集中選擇任何列。IndexError:索引2超出軸1的範圍,大小爲

這裏是我的代碼:

url = "http://ww2.amstat.org/publications/jse/datasets/fishcatch.dat.txt" 
df = pd.read_csv(url, sep= '\t', header= 0) 

print df 

     1  1  242.0  23.2 25.4 30.0 38.4 13.4 NA 
0  2  1  290.0  24.0 26.3 31....     
1  3  1  340.0  23.9 26.5 31....     
2  4  1  363.0  26.3 29.0 33....     
3  5  1  430.0  26.5 29.0 34.... 

這是我得到的,當我嘗試在數據集中訪問第3列

df[:,2] 

IndexErrorTraceback (most recent call last) 
<ipython-input-27-910d22bca5b5> in <module>() 
    ----> 1 df[:,2] 

IndexError: index 2 is out of bounds for axis 1 with size 1 

我是新來的編碼和使用python 2 jupyter錯誤筆記本。任何形式的幫助,將不勝感激。

+0

你需要'iloc'。 'df.iloc [:,2]' – Psidom

回答

0

要加載的數據幀,使用

df = pd.read_csv(url, delim_whitespace=True, header=0) 

看來你的數據不是簡單的製表符分隔。因爲熊貓不能正確分析數據,所有內容都被初始化爲一列。

此外,要訪問第3列,應該使用df.iloc[:, 2]

相關問題