繁体   English   中英

Python map()函数输出到Pandas DataFrame中

[英]Python map() function output into Pandas DataFrame

我利用python的map()函数将参数传递给交易模型并输出结果。 我使用itertools.product来查找两个参数的所有可能组合,然后将组合传递给名为“run”的函数。 函数run返回一个返回的pandas数据帧。 Column标头是两个参数的元组和返回的sharpe比率。 见下文:

def run((x,y)): 
    ENTRYMULT = x
    PXITR1PERIOD = y

    create_trade()
    pull_settings()    
    pull_marketdata()
    create_position()
    create_pnl_output()

    return DataFrame(DF3['NETPNL'].values, index=DF3.index, columns=[(ENTRYMULT,PXITR1PERIOD,SHARPE)])

我的main()函数使用Pool()功能在所有8个核心上运行map():

if __name__ == '__main__':    
global DF3
pool = Pool()    
test1 =pool.map(run,list(itertools.product([x * 0.1 for x in range(10,12)], range(100,176,25))))
print test1

我意识到map函数只能输出列表。 输出是返回数据帧的标题列表。来自print test1的输出如下所示:

[(1.0, 150, -8.5010673966997263)
2011-11-17  18.63                          
2011-11-18  17.86                          
2011-11-21  17.01                          
2011-11-22  15.92                          
2011-11-23  15.56                          
2011-11-24  15.56                          
2011-11-25  15.36                          
2011-11-28  15.18                          
2011-11-29  15.84                          
2011-11-30  NaN                            ,             (1.0, 175, -9.4016837593189102)
2011-11-17  22.63                          
2011-11-18  22.03                          
2011-11-21  21.36                          
2011-11-22  19.93                          
2011-11-23  19.77                          
2011-11-24  19.77                          
2011-11-25  19.68                          
2011-11-28  19.16                          
2011-11-29  19.56                          
2011-11-30  NaN                            ,             (1.1, 100, -20.255968672741457)
2011-11-17  12.03                          
2011-11-18  10.95                          
2011-11-21  10.03                          
2011-11-22  9.003                          
2011-11-23  8.221                          
2011-11-24  8.221                          
2011-11-25  7.903                          
2011-11-28  7.709                          
2011-11-29  6.444                          
2011-11-30  NaN                            ,             (1.1, 125, -18.178187305758119)
2011-11-17  14.64                          
2011-11-18  13.76                          
2011-11-21  12.89                          
2011-11-22  11.85                          
2011-11-23  11.34                          
2011-11-24  11.34                          
2011-11-25  11.16                          
2011-11-28  11.06                          
2011-11-29  10.14                          
2011-11-30  NaN                            ,             (1.1, 150, -14.486791104380069)
2011-11-17  26.25                          
2011-11-18  25.57                          
2011-11-21  24.76                          
2011-11-22  23.74                          
2011-11-23  23.48                          
2011-11-24  23.48                          
2011-11-25  23.43                          
2011-11-28  23.38                          
2011-11-29  22.93                          
2011-11-30  NaN                            ,             (1.1, 175, -12.118290962161304)
2011-11-17  24.66                          
2011-11-18  24.21                          
2011-11-21  23.57                          
2011-11-22  22.14                          
2011-11-23  22.06                          
2011-11-24  22.06                          
2011-11-25  22.11                          
2011-11-28  21.64                          
2011-11-29  21.24                          
2011-11-30  NaN                            ] 

我的最终目标是让一个带有索引的pandas数据框(对于所有返回都相同),(ENTRYMULT,PXITR1PERIOD,SHARPE)的列标题以及下面的相应返回。 然后,我将在所有返回系列中进行成对相关计算。

我想你需要做的就是:

data = DataFrame(dict(test1))

这将导致DataFrame的列是像(1.1,175,-12.118290962161304)这样的元素

在pandas 0.6.1(即将发布)中,您还可以:

data = DataFrame.from_items(test1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM