2015-09-19 80 views
2

我試圖用一些電子表格(.xlsx)中的一些數據使用scikit。爲了達到這個目的,我使用Pandas來閱讀電子表格,然後我將使用numpy來使用scikit。用熊貓創建NumPy數組

這裏的問題是,當我將DF結構轉換爲numpy時,我幾乎丟失了所有的數據!我認爲這是因爲它沒有列名。只有原始數據。 EX:

28.7967 16.0021 2.6449 0.3918 0.1982

31.6036 11.7235 2.5185 0.5303 0.3773

162.052 136.031 4.0612 0.0374 0.0187

我迄今爲止代碼:

def split_data(): 
    test_data = pd.read_excel('magic04.xlsx', sheetname=0, skip_footer=16020) 
    #code below prints correctly the data 
    print test_data.iloc[:, 0:10] 

    #none of the code below work as expected 
    test1 = np.array(test_data.iloc[:, 0:10]) 
    test2 = test_data.as_matrix() 

我真的失去了這裏。任何幫助將非常歡迎...

回答

2

我建議你使用header=Noneread_excel。看到以下內容:

df = pd.read_excel('stuff.xlsx') 
>> df 
    28.7967 16.0021 2.6449 0.3918 0.1982 
0 31.6036 11.7235 2.5185 0.5303 0.3773 
1 162.0520 136.0310 4.0612 0.0374 0.0187 

>> df.ix[:, 1: 2] 

0 
1 

對戰:

df = pd.read_excel('stuff.xlsx', header=None) 
>> df 

0 1 2 3 4 
0 28.7967 16.0021 2.6449 0.3918 0.1982 
1 31.6036 11.7235 2.5185 0.5303 0.3773 
2 162.0520 136.0310 4.0612 0.0374 0.0187 

>> df.ix[:, 1: 2] 
    1 2 
0 16.0021 2.6449 
1 11.7235 2.5185 
2 136.0310 4.0612 
+1

這個工作!它以兩種方式工作:屬性「.iloc [:, 0:X]」和方法「as_matrix()」!真的感謝! – mk2