2015-09-19 84 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