这是我的numpy数组:
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>
我尝试过这样做,使用pandas:
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 :(得分:7)
更新:确保big_array
有4列。 big_array
的形状与样本数组num
的形状不匹配。这就是示例代码正常工作的原因,但不是真正的代码。
我无法重现您的错误消息。在我的系统上(Windows,Python 2.7,pandas-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()