如何在python的数据框中获取函数(def)输出并合并多个数据框

时间:2018-09-30 17:41:12

标签: python

我有这段代码可以使用股票价格计算最大跌幅。

#max drawdown function code...
def max_drawdown(X):
    mdd = 0
    peak = X[0]
    for x in X:
        if x > peak: 
            peak = x
        dd = (x - peak) / peak
        if dd < mdd
            mdd = dd

    return mdd

我使用以下功能来显示跌幅

#shows max drawdown
stocks_prices.xs(key='close',axis=1,level='Stock Info').apply(max_drawdown)

如果您想查看股价的高低

Ticker  NOW BBY NOW Return  BBY Return
Stock Info  open    high    low close   volume  open    high    low close   volume      
date                                                
2013-09-30  52.10   52.34   51.170  51.95   1383145 31.9605 32.3721 31.8061 32.1577 3826963 NaN NaN
2013-10-01  51.60   51.89   50.610  51.49   1288635 32.2092 32.7923 32.1834 32.5436 3607267 -0.008855   0.012000
2013-10-02  51.35   52.42   51.215  52.27   1158196 32.2606 32.4750 31.9005 32.2949 2990664 0.015149    -0.007642
2013-10-03  52.39   52.90   51.560  52.15   1512797 32.4236 32.5093 31.6089 32.3206 3413673 -0.002296   0.000796
2013-10-04  53.09   55.46   52.810  54.43   1688824 32.3206 32.9724 31.9862 32.7151 3311713 0.043720    0.012206

输出为

Ticker
NOW   -0.476164
BBY   -0.485378
dtype: float64

由于计算在函数内部,因此如何在数据框中输入输出。我无法使用以下代码从下拉功能的输出中创建数据框

mdd = mdd.to_frame('Maximum Drawdown')
mdd.index.name = 'Ticker'
mdd = mdd.reset_index()

我有一些类似的数据帧

 Ticker    Sharpe
0    NOW  8.061887
1    BBY  7.174034

Ticker    Var
0    NOW  -0.1
1    BBY  0.2

我如何将所有这些数据帧合并为一个,以便可以从代码开始,然后从值开始导出到excel

例如,我希望输出为

Ticker    Var   Sharpe Ratio
    0    NOW  -0.1 0.5
    1    BBY  0.2 0.3

请在这两个方面提供帮助,非常感谢

1 个答案:

答案 0 :(得分:0)

所以从您的代码开始:

mdd = stocks_prices.xs(key='close',axis=1,level='Stock Info').apply(max_drawdown)

mdd系列看起来像:

Ticker
NOW   -0.008855
BBY   -0.007642
dtype: float64

您可能有不同的电话号码。

不确定是否要在stocks_prices中使用-如果这样做,可以这样做:

最简单的方法是创建一个新列

stocks_prices[ 'NOW mdd' ] = float('NaN')

并获取数据框的最后一天:

last_day = stocks_prices.index[-1]

对我来说是2013-10-04,但我认为您还有更多行。无论如何,请将其粘贴到列NOW mdd的最后一行:

stocks_prices.loc[ last_day, 'NOW mdd' ] = mdd[ 'NOW' ]

所以我的stocks_prices如下:

Ticker        NOW                       BBY                    NOW Return BBY Return   NOW mdd
Stock Info   open high  low  close     open high  low    close
2013-09-30  50.95  NaN  NaN  51.95  31.1577  NaN  NaN  32.1577        NaN        NaN       NaN
2013-10-01  50.49  NaN  NaN  51.49  31.5436  NaN  NaN  32.5436  -0.008855   0.012000       NaN
2013-10-02  51.27  NaN  NaN  52.27  31.2949  NaN  NaN  32.2949   0.015149  -0.007642       NaN
2013-10-03  51.15  NaN  NaN  52.15  31.3206  NaN  NaN  32.3206  -0.002296   0.000796       NaN
2013-10-04  53.43  NaN  NaN  54.43  31.7151  NaN  NaN  32.7151   0.043720   0.012206 -0.008855

合并

将DataFrame放在一起通常是在诸如Ticker之类的通用键上完成的。

因此,一个sharpe数据框:

     Sharpe Ticker
0  8.061887    NOW
1  7.174034    BBY

和一个var数据框:

  Ticker  Var
0    NOW -0.1
1    BBY  0.2

可以与

合并
result = sharpe.merge( var, on='Ticker', suffixes=('','') )

给予

     Sharpe Ticker  Var
0  8.061887    NOW -0.1
1  7.174034    BBY  0.2

如果我们想将mdd合并到其中,我们可以先将mdd变成一个DataFrame:

result2 = result.merge( mdd.to_frame('mdd'), on='Ticker', suffixes=('','') )

给予

     Sharpe Ticker  Var       mdd
0  8.061887    NOW -0.1 -0.008855
1  7.174034    BBY  0.2 -0.007642

连续最大跌幅

您可以立即更改max_drawdown函数以输出列表,例如

def max_drawdown2(X):
    mdd = 0
    peak = X[0]
    values = []  # <-- NEW LINE
    for x in X:
        if x > peak: 
            peak = x
        dd = (x - peak) / peak
        if dd < mdd:
            mdd = dd
        values.append( mdd )  # <-- NEW LINE

    return values   # <-- NEW LINE

stocks_prices[ 'NOW mdd' ] = max_drawdown2( stocks_prices.loc[ :, ('NOW','close') ] )

获得

Ticker        NOW                       BBY                    NOW Return BBY Return   NOW mdd
Stock Info   open high  low  close     open high  low    close
2013-09-30  50.95  NaN  NaN  51.95  31.1577  NaN  NaN  32.1577        NaN        NaN  0.000000
2013-10-01  50.49  NaN  NaN  51.49  31.5436  NaN  NaN  32.5436  -0.008855   0.012000 -0.008855
2013-10-02  51.27  NaN  NaN  52.27  31.2949  NaN  NaN  32.2949   0.015149  -0.007642 -0.008855
2013-10-03  51.15  NaN  NaN  52.15  31.3206  NaN  NaN  32.3206  -0.002296   0.000796 -0.008855
2013-10-04  53.43  NaN  NaN  54.43  31.7151  NaN  NaN  32.7151   0.043720   0.012206 -0.008855

请注意,.loc[ :, ('NOW','close') ]正在使用:获取所有日期,然后('NOW','close')正在获取Ticker然后是Stock Info

可以使用相同的(Ticker, close)语法来添加子列:

stocks_prices[ ('NOW', 'mdd' ) ] = max_drawdown2( stocks_prices.loc[ :, ('NOW','close') ] )