【问题标题】:Consolidate and transform pd.df based on condition根据条件合并和转换 pd.df
【发布时间】:2017-10-11 08:27:54
【问题描述】:

我正在寻找合并以下数据框,如下所示。 df

       Expiry           F      K Type     sigma
0  2017-10-27  125.109375  123.5    P  0.045410
1  2017-10-27  125.109375  127.5    P  0.047965
2  2017-10-27  125.109375  124.5    P  0.041822
3  2017-10-27  125.109375  125.5    P  0.041526
4  2017-10-27  125.109375  120.5    P  0.045410
5  2017-10-27  125.109375  121.5    P  0.045410
6  2017-10-27  125.109375  121.0    P  0.045410
7  2017-10-27  125.109375  122.0    P  0.045410
8  2017-10-27  125.109375  123.0    P  0.045410
9  2017-10-27  125.109375  124.0    P  0.043341
10 2017-10-27  125.109375  125.0    P  0.041143
11 2017-10-27  125.109375  126.0    P  0.043123
12 2017-10-27  125.109375  127.0    P  0.047965
13 2017-10-27  125.109375  128.0    P  0.047965
14 2017-10-27  125.109375  128.5    P  0.047965
15 2017-10-27  125.109375  129.0    P  0.047965
16 2017-10-27  125.109375  129.5    P  0.047965
17 2017-10-27  125.109375  130.0    P  0.047965
18 2017-10-27  125.109375  126.5    P  0.046020
19 2017-10-27  125.109375  122.5    P  0.045410
20 2017-10-27  125.109375  123.5    C  0.045410
21 2017-10-27  125.109375  127.5    C  0.047965
22 2017-10-27  125.109375  124.5    C  0.041822
23 2017-10-27  125.125000  125.5    C  0.041629
24 2017-10-27  125.125000  120.5    C  0.045487
25 2017-10-27  125.125000  121.5    C  0.045487
26 2017-10-27  125.125000  121.0    C  0.045487
27 2017-10-27  125.125000  122.0    C  0.045487
28 2017-10-27  125.125000  123.0    C  0.045487
29 2017-10-27  125.125000  124.0    C  0.043292
..        ...         ...    ...  ...       ...
70 2017-11-03  125.109375  125.0    C  0.040830
71 2017-11-03  125.109375  126.0    C  0.042517
72 2017-11-03  125.109375  127.0    C  0.046631
73 2017-11-03  125.109375  128.0    C  0.046631
74 2017-11-03  125.109375  128.5    C  0.046631
75 2017-11-03  125.109375  129.0    C  0.046631
76 2017-11-03  125.109375  129.5    C  0.046631
77 2017-11-03  125.109375  130.0    C  0.046631
78 2017-11-03  125.109375  126.5    C  0.044948
79 2017-11-03  125.109375  122.5    C  0.044366
80 2017-10-20  125.109375  123.5    P  0.046512
81 2017-10-20  125.109375  127.5    P  0.048400
82 2017-10-20  125.109375  124.5    P  0.041512
83 2017-10-20  125.109375  125.5    P  0.042744
84 2017-10-20  125.109375  120.5    P  0.046512
85 2017-10-20  125.109375  121.5    P  0.046512
86 2017-10-20  125.109375  121.0    P  0.046512
87 2017-10-20  125.109375  122.0    P  0.046512
88 2017-10-20  125.109375  123.0    P  0.046512
89 2017-10-20  125.109375  124.0    P  0.044166
90 2017-10-20  125.109375  125.0    P  0.041220
91 2017-10-20  125.109375  126.0    P  0.045406
92 2017-10-20  125.109375  127.0    P  0.048400
93 2017-10-20  125.109375  128.0    P  0.048400
94 2017-10-20  125.109375  128.5    P  0.048400
95 2017-10-20  125.109375  129.0    P  0.048400
96 2017-10-20  125.109375  129.5    P  0.048400
97 2017-10-20  125.109375  130.0    P  0.048400
98 2017-10-20  125.109375  126.5    P  0.048400
99 2017-10-20  125.109375  122.5    P  0.046512

根据以下条件选择 Sigma: 如果 F > K 使用 Type=P 的 sigma 否则对 type=C 使用 sigma 我正在寻找的结果应该类似于以下示例:

              123  123.5    124  124.5    125  125.5    126  126.5 
Expiry                                                                         
2017-10-20  0.051  0.047  0.043   0.04  0.040  0.039  0.041  0.043
2017-10-27  0.045  0.041  0.041   0.04  0.039  0.039  0.040  0.042
.....

我们将非常感谢您的帮助。

【问题讨论】:

    标签: pandas boolean python-3.6


    【解决方案1】:
    # create auxiliary column to check condition option 1
    df['to_use'] = df.apply(lambda x: 1 if (x['F'] > x['K'] and x['Type'] == 'P') or x['Type'] == 'C' else 0, axis = 1)
    # create auxiliary column to check condition option 2 (fastest option)
    df['to_use'] = (df['F'] > df['K'])*1*(df['Type'] == 'P' )*1 + (df['Type'] == 'C' )*1
    
    # sort data to place condition match values on top and P-type higher than C-type
    df.sort_values(by = ['Expiry', 'K', 'to_use', 'Type'], ascending = [True, True, False, False], inplace = True)
    
    # leave only values matching to condition (if P-type than it'll be higher otherwise C-type
    new_df = df.drop_duplicates(subset = ['Expiry', 'K'], keep = 'first')
    
    # now we are done to present result as a pivot table
    new_df.pivot_table(index='Expiry', columns='K', values = 'sigma').reset_index()
    

    【讨论】:

    • 非常感谢。效果很好。话虽如此,我可能会用布尔检查重写第一行,以免使用 apply ,这在我的经验中很慢。
    • 您能接受我的解决方案作为答案吗? =) 谢谢!
    • 我还为条件添加了选项 2
    • 谢谢!公式中的那些 1 是不必要的,这会引发 UserWarning。 numexpr 不支持 '' 运算符用于 bool dtype,使用 '&' 代替 numexpr 不支持'+' 运算符用于 bool dtype,使用 '|'而是
    • 这项工作也是如此:df['use'] = ((df['F'] > df['K']) & (df['Type'] == 'P')) | ((df['F']
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多