2013-10-28 27 views
1

这是我的numpy的数组:ValueError异常:传递的值的形状是(3,27),指数意味着(4,27)#熊猫数据帧

import numpy as np 
num = np.array([[ 0.17899619 0.33093259 0.2076353 0.06130814] 
       [ 0.20392888 0.42653105 0.33325891 0.10473969] 
       [ 0.17038247 0.19081956 0.10119709 0.09032416] 
       [-0.10606583 -0.13680513 -0.13129103 -0.03684349] 
       [ 0.20319428 0.28340985 0.20994867 0.11728491] 
       [ 0.04396872 0.23703525 0.09359683 0.11486036] 
       [ 0.27801304 -0.05769304 -0.06202813 0.04722761]]) 

这是我的标题行:

days = ['5 days', '10 days', '20 days', '60 days'] 

这里是我的第一列:

prices = ['AAPL', 'ADBE', 'AMD', 'AMZN', 'CRM', 'EXPE', 'FB'] 

我想把这一切在一个HTML表是这样的:

<table border="1" class="dataframe"> 
    <thead> 
    <tr style="text-align: right;"> 
     <th></th> 
     <th>5 days</th> 
     <th>10 days</th> 
     <th>20 days</th> 
     <th>60 days</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
     <th>AAPL</th> 
     <td> 0.178996</td> 
     <td> 0.330933</td> 
     <td> 0.207635</td> 
     <td> 0.061308</td> 
    </tr> 
    <tr> 
     <th>ADBE</th> 
     <td> 0.203929</td> 
     <td> 0.426531</td> 
     <td> 0.333259</td> 
     <td> 0.104740</td> 
    </tr> 
    <tr> 
     <th>AMD</th> 
     <td> 0.170382</td> 
     <td> 0.190820</td> 
     <td> 0.101197</td> 
     <td> 0.090324</td> 
    </tr> 
    <tr> 
     <th>AMZN</th> 
     <td>-0.106066</td> 
     <td>-0.136805</td> 
     <td>-0.131291</td> 
     <td>-0.036843</td> 
    </tr> 
    <tr> 
     <th>CRM</th> 
     <td> 0.203194</td> 
     <td> 0.283410</td> 
     <td> 0.209949</td> 
     <td> 0.117285</td> 
    </tr> 
    <tr> 
     <th>EXPE</th> 
     <td> 0.043969</td> 
     <td> 0.237035</td> 
     <td> 0.093597</td> 
     <td> 0.114860</td> 
    </tr> 
    <tr> 
     <th>FB</th> 
     <td> 0.278013</td> 
     <td>-0.057693</td> 
     <td>-0.062028</td> 
     <td> 0.047228</td> 
    </tr> 
    </tbody> 
</table> 

我试图做到这一点的方式,利用大熊猫:

import pandas as pd 
df = pd.DataFrame(num, index=prices, columns=days) 
html = df.to_html() 
print html 

但是当我运行这段代码,我有以下错误:

Traceback (most recent call last): 
    File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 2226, in create_block_manager_from_blocks 
    blocks = [ make_block(blocks[0], axes[0], axes[0], placement=placement) ] 
    File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 967, in make_block 
    return klass(values, items, ref_items, ndim=values.ndim, fastpath=fastpath, placement=placement) 
    File "C:\Python33\lib\site-packages\pandas\core\internals.py", line 45, in __init__ 
    % (len(items), len(values))) 
ValueError: Wrong number of items passed 4, indices imply 3 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 261, in <module> 
    main() 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 42, in main 
    print (html_table(returns_array, list_of_days, [index] + security_list)) 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 185, in html_table 
    df = pd.DataFrame(big_array, index=companies, columns=days) 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 415, in __init__ 
    copy=copy) 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 561, in _init_ndarray 
    return create_block_manager_from_blocks([ values.T ], [ columns, index ]) 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 2235, in create_block_manager_from_blocks 
    construction_error(tot_items,blocks[0].shape[1:],axes) 
    File "C:\Users\user\Documents\progfun\finance\file.py", line 2217, in construction_error 
    tuple(map(int, [len(ax) for ax in axes])))) 
ValueError: Shape of passed values is (3, 27), indices imply (4, 27) 

我该如何解决呢?

+0

什么是big_array'的'形状,'companies'和'days'? (例如'big_array.shape'等) – Matt

+0

big_array == num; big_array.shape ==(27,3) –

+0

我相应地更新了我的答案...... – Matt

回答

5

更新:确保big_array有4列。 big_array的形状与您的样本阵列num的形状不匹配。这就是为什么示例代码正在工作,但您的真实代码不是。


我无法重现您的错误消息。在我的系统(Windows中,Python 2.7版,熊猫-0.11.0,numpy的-1.7.1),一切运行下面的代码时,按预期工作:

import numpy as np 
import pandas as pd 

num = np.array([[ 0.17899619, 0.33093259, 0.2076353, 0.06130814], 
       [ 0.20392888, 0.42653105, 0.33325891, 0.10473969], 
       [ 0.17038247, 0.19081956, 0.10119709, 0.09032416], 
       [-0.10606583, -0.13680513, -0.13129103, -0.03684349], 
       [ 0.20319428, 0.28340985, 0.20994867, 0.11728491], 
       [ 0.04396872, 0.23703525, 0.09359683, 0.11486036], 
       [ 0.27801304, -0.05769304, -0.06202813, 0.04722761]]) 

days = ['5 days', '10 days', '20 days', '60 days'] 
prices = ['AAPL', 'ADBE', 'AMD', 'AMZN', 'CRM', 'EXPE', 'FB'] 

print pd.DataFrame(num, index=prices, columns=days).to_html()