这是我的 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>
我试图这样做,使用熊猫:
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)
我该如何解决?