【发布时间】:2022-01-12 00:16:32
【问题描述】:
下面的代码在下面给出了一个错误,我怎样才能创建一个模块化函数来结合外部和内部字典值并从中生成一个 pandas 数据框?
代码:
import pandas as pd
import numpy as np
a = {'Long and Short PnL': np.array([ 0. , 0. , 0. , 0. ,1
0. , 0. , 0. , 0. ,2
0. , 0. , 0. , 0. ,3
0. , 0. , 0. , 0. ,4
0. , 0. , 0. , 0. ,5
0. , 0. , 0. , 0. ,6
-8.31004521, 0. , 0. , 315.29767864,7
-13.49807721, -28.64143208, -16.60131942, 0. ]),
'Long and Short Max Upside PnL': np.array([ 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0.85, 0. , 0. , 602.5 ,
25.7 , 0. , 96.45, 120.8]),
'Long and Short Compounded': {'Last Capitals': np.array([ 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
91.86164693, 0. , 0. , 329.05735514,
401.83251206, 79.712739 , 75.76562339, 0. ]),
'Max Drawdown': np.array([ 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. ,
91.86164693, 0. , 0. , 96.15319621,
401.83251206, 79.712739 , 75.76562339, 0. ])}}
display(pd.DataFrame(a))
错误代码:
ValueError: All arrays must be of the same length
预期输出:
【问题讨论】:
标签: python pandas database dataframe numpy