在GroupBy

时间:2015-06-22 21:30:19

标签: pandas triggers group-by

EdChum提供的问题几乎是一个优雅的答案。该问题与此问题之间的区别在于,现在需要将封顶应用于已执行“GroupBy”的数据。

原始数据:

Symbol  DTE     Spot    Strike  Vol
AAPL    30.00   100.00  80.00   14.58
AAPL    30.00   100.00  85.00   16.20
AAPL    30.00   100.00  90.00   18.00
AAPL    30.00   100.00  95.00   20.00
AAPL    30.00   100.00  100.00  22.00
AAPL    30.00   100.00  105.00  25.30
AAPL    30.00   100.00  110.00  29.10
AAPL    30.00   100.00  115.00  33.46
AAPL    30.00   100.00  120.00  38.48
AAPL    50.00   102.00  80.00   13.08
AAPL    50.00   102.00  85.00   14.70
AAPL    50.00   102.00  90.00   16.50
AAPL    50.00   102.00  95.00   18.50
AAPL    50.00   102.00  100.00  20.50
AAPL    50.00   102.00  105.00  23.80
AAPL    50.00   102.00  110.00  27.60
AAPL    50.00   102.00  115.00  31.96
AAPL    50.00   102.00  120.00  36.98
 IBM    30.00   170.00  150.00  7.29
 IBM    30.00   170.00  155.00  8.10
 IBM    30.00   170.00  160.00  9.00
 IBM    30.00   170.00  165.00  10.00
 IBM    30.00   170.00  170.00  11.00
 IBM    30.00   170.00  175.00  12.65
 IBM    30.00   170.00  180.00  14.55
 IBM    30.00   170.00  185.00  16.73
 IBM    30.00   170.00  190.00  19.24
 IBM    60.00   171.00  150.00  5.79
 IBM    60.00   171.00  155.00  6.60
 IBM    60.00   171.00  160.00  7.50
 IBM    60.00   171.00  165.00  8.50
 IBM    60.00   171.00  170.00  9.50
 IBM    60.00   171.00  175.00  11.15
 IBM    60.00   171.00  180.00  13.05
 IBM    60.00   171.00  185.00  15.23
 IBM    60.00   171.00  190.00  17.74

然后我创建了一些新变量:

df['ATM_dist'] =abs(df['Spot']-df['Strike'])

imin = df.groupby(['DTE','Symbol'])['ATM_dist'].transform('idxmin')

df['NormStrike']=np.log(df['Strike']/df['Spot'])/(((df['DTE']/365)**.5)*df['ATMvol']/100)

df['ATMvol'] = df.loc[imin,'Vol'].values

结果如下:

   Symbol  DTE  Spot  Strike    Vol  ATM_dist  ATMvol  NormStrike
0    AAPL   30   100      80  14.58        20    22.0   -3.537916
1    AAPL   30   100      85  16.20        15    22.0   -2.576719
2    AAPL   30   100      90  18.00        10    22.0   -1.670479
3    AAPL   30   100      95  20.00         5    22.0   -0.813249
4    AAPL   30   100     100  22.00         0    22.0    0.000000
5    AAPL   30   100     105  25.30         5    22.0    0.773562
6    AAPL   30   100     110  29.10        10    22.0    1.511132
7    AAPL   30   100     115  33.46        15    22.0    2.215910
8    AAPL   30   100     120  38.48        20    22.0    2.890688
9    AAPL   50   102      80  13.08        22    20.5   -3.201973
10   AAPL   50   102      85  14.70        17    20.5   -2.402955
11   AAPL   50   102      90  16.50        12    20.5   -1.649620
12   AAPL   50   102      95  18.50         7    20.5   -0.937027
13   AAPL   50   102     100  20.50         2    20.5   -0.260994
14   AAPL   50   102     105  23.80         3    20.5    0.382049
15   AAPL   50   102     110  27.60         8    20.5    0.995172
16   AAPL   50   102     115  31.96        13    20.5    1.581035
17   AAPL   50   102     120  36.98        18    20.5    2.141961
18    IBM   30   170     150   7.29        20    11.0   -3.968895
19    IBM   30   170     155   8.10        15    11.0   -2.929137
20    IBM   30   170     160   9.00        10    11.0   -1.922393
21    IBM   30   170     165  10.00         5    11.0   -0.946631
22    IBM   30   170     170  11.00         0    11.0    0.000000
23    IBM   30   170     175  12.65         5    11.0    0.919188
24    IBM   30   170     180  14.55        10    11.0    1.812480
25    IBM   30   170     185  16.73        15    11.0    2.681295
26    IBM   30   170     190  19.24        20    11.0    3.526940
27    IBM   60   171     150   5.79        21     9.5   -3.401827
28    IBM   60   171     155   6.60        16     9.5   -2.550520
29    IBM   60   171     160   7.50        11     9.5   -1.726243
30    IBM   60   171     165   8.50         6     9.5   -0.927332
31    IBM   60   171     170   9.50         1     9.5   -0.152273
32    IBM   60   171     175  11.15         4     9.5    0.600317
33    IBM   60   171     180  13.05         9     9.5    1.331704
34    IBM   60   171     185  15.23        14     9.5    2.043051
35    IBM   60   171     190  17.74        19     9.5    2.735427

我希望将'Vol'上限的值设置为另一列'NormStrike'触发触发器的水平(在这种情况下为abs(NormStrike)> = 2)。创建了这个新列'Desired_Level',同时保持'Vol'列保持不变。第一个上限应该使索引位置0的Vol值为16.2,因为当NormStrike达到-2.576719时,在索引位置1触发上限。

补充说明: 我正在寻找一种通用解决方案,它可以在两个方向上远离最低abs(NormStrike)水平,同时达到-2和+2触发。如果没有命中(它可能不是),那么所需的级别就是original_level

另外需要注意的是,abs(NormStrike)的大小从min(abs(NormStrike))水平继续增长,因为它是abs(从点到点的距离)的函数

EdChum提供的代码(在我将GroupBy引入混合之前)如下:

clip = 4
lower = df.loc[df['NS'] <= -clip, 'Vol'].idxmax() 
upper = df.loc[df['NS'] >= clip, 'Vol'].idxmin()


df['Original_level'] = df['Original_level'].clip(df.loc[lower,'Original_level'], df.loc[upper, 'Original_level'])

有两个问题,第一,它在groupby之后不起作用,第二个,如果一组特定的数据没有超过“clip”值的NS值,那么它会产生错误。理想的结果是,在这种情况下,对于特定符号/ DTE组的Vol级别没有任何处理。

Ed建议实施reset_index(),但我不知道如何使用它来解决问题。

我希望这不是一个错综复杂的问题

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

你可以试试看它是否有用。我假设如果剪辑已被触发,那么将放置NaN。您可以通过自定义选项替换它。

import pandas as pd
import numpy as np

# use np.where(criterion, x, y) to do a vectorized statement like if criterion is True, then set it to x, else set it to y

def func(group):
    group['Triggered'] = np.where((group['NormStrike'] >= 2) | (group['NormStrike'] <= -4), 'Yes', 'No')
    group['Desired_Level'] = np.where((group['NormStrike'] >= 2) | (group['NormStrike'] <= -4), np.nan, group['Vol'])
    group = group.fillna(method='ffill').fillna(method='bfill')
    return group

df = df.groupby(['Symbol', 'DTE']).apply(func)

Out[410]: 
   Symbol  DTE  Spot  Strike    Vol  ATM_dist  ATMvol  NormStrike Triggered  Desired_Level
0    AAPL   30   100      80  14.58        20      22     -3.5379        No          14.58
1    AAPL   30   100      85  16.20        15      22     -2.5767        No          16.20
2    AAPL   30   100      90  18.00        10      22     -1.6705        No          18.00
3    AAPL   30   100      95  20.00         5      22     -0.8132        No          20.00
4    AAPL   30   100     100  22.00         0      22      0.0000        No          22.00
5    AAPL   30   100     105  25.30         5      22      0.7736        No          25.30
6    AAPL   30   100     110  29.10        10      22      1.5111        No          29.10
7    AAPL   30   100     115  33.46        15      22      2.2159       Yes          29.10
8    AAPL   30   100     120  38.48        20      22      2.8907       Yes          29.10
9    AAPL   50   102      80  14.58        22      22     -3.5379        No          14.58
10   AAPL   50   102      85  16.20        17      22     -2.5767        No          16.20
11   AAPL   50   102      90  18.00        12      22     -1.6705        No          18.00
12   AAPL   50   102      95  20.00         7      22     -0.8132        No          20.00
13   AAPL   50   102     100  22.00         2      22      0.0000        No          22.00
14   AAPL   50   102     105  25.30         3      22      0.7736        No          25.30
15   AAPL   50   102     110  29.10         8      22      1.5111        No          29.10
16   AAPL   50   102     115  33.46        13      22      2.2159       Yes          29.10
17   AAPL   50   102     120  38.48        18      22      2.8907       Yes          29.10
18   AAPL   30   170     150  14.58        20      22     -3.5379        No          14.58
19   AAPL   30   170     155  16.20        15      22     -2.5767        No          16.20
20   AAPL   30   170     160  18.00        10      22     -1.6705        No          18.00
21   AAPL   30   170     165  20.00         5      22     -0.8132        No          20.00
22   AAPL   30   170     170  22.00         0      22      0.0000        No          22.00
23   AAPL   30   170     175  25.30         5      22      0.7736        No          25.30
24   AAPL   30   170     180  29.10        10      22      1.5111        No          29.10
25   AAPL   30   170     185  33.46        15      22      2.2159       Yes          29.10
26   AAPL   30   170     190  38.48        20      22      2.8907       Yes          29.10
27   AAPL   60   171     150  14.58        21      22     -3.5379        No          14.58
28   AAPL   60   171     155  16.20        16      22     -2.5767        No          16.20
29   AAPL   60   171     160  18.00        11      22     -1.6705        No          18.00
30   AAPL   60   171     165  20.00         6      22     -0.8132        No          20.00
31   AAPL   60   171     170  22.00         1      22      0.0000        No          22.00
32   AAPL   60   171     175  25.30         4      22      0.7736        No          25.30
33   AAPL   60   171     180  29.10         9      22      1.5111        No          29.10
34   AAPL   60   171     185  33.46        14      22      2.2159       Yes          29.10
35   AAPL   60   171     190  38.48        19      22      2.8907       Yes          29.10