根据复杂条件将列添加到pandas数据透视表

时间:2018-01-11 04:44:19

标签: pandas python-3.6

我的数据透视表看起来像这样:

In [285]: piv
Out[285]: 
K              118.5     119.0     119.5     120.0     120.5
Expiry                                                      
2018-01-12  0.050842  0.050842  0.050842  0.050842  0.050842
2018-01-19  0.039526  0.039526  0.039526  0.039526  0.039526
2018-01-26  0.039196  0.039196  0.039196  0.039196  0.039196
2018-02-02  0.039991  0.039991  0.039991  0.039991  0.039991
2018-02-23  0.040005  0.040005  0.040005  0.040005  0.040005
2018-03-23  0.041025  0.041000  0.040872  0.040623  0.040398

和df2看起来像这样:

In [290]: df2
Out[290]: 
                              F       Symbol
Expiry                                      
2018-03-20 12:00:00  123.000000  ZN   MAR 18
2018-06-20 12:00:00  122.609375  ZN   JUN 18

我希望根据以下内容添加piv ['F']: piv.index.month< df2.index.month

所以结果应如下所示:

K          F              118.5     119.0     119.5     120.0     120.5
Expiry                                                      
2018-01-19 123.000000  0.039526  0.039526  0.039526  0.039526  0.039526
2018-01-26 123.000000  0.039196  0.039196  0.039196  0.039196  0.039196
2018-02-02 123.000000  0.039991  0.039991  0.039991  0.039991  0.039991
2018-02-23 123.000000  0.040005  0.040005  0.040005  0.040005  0.040005
2018-03-23 123.609375  0.041025  0.041000  0.040872  0.040623  0.040398

将非常感谢。

2 个答案:

答案 0 :(得分:3)

reindex + backfill

df.index=pd.to_datetime(df.index)
df1.index=pd.to_datetime(df1.index)

df['F']=df1.reindex(df.index,method='backfill').F.values
df
Out[164]: 
               118.5     119.0     119.5     120.0     120.5           F
2018-01-12  0.050842  0.050842  0.050842  0.050842  0.050842  123.000000
2018-01-19  0.039526  0.039526  0.039526  0.039526  0.039526  123.000000
2018-01-26  0.039196  0.039196  0.039196  0.039196  0.039196  123.000000
2018-02-02  0.039991  0.039991  0.039991  0.039991  0.039991  123.000000
2018-02-23  0.040005  0.040005  0.040005  0.040005  0.040005  123.000000
2018-03-23  0.041025  0.041000  0.040872  0.040623  0.040398  122.609375

答案 1 :(得分:2)

您希望pd.merge_asofdirection='forward'一起使用,并确保合并索引。

pd.merge_asof(
    piv, df2[['F']],
    left_index=True,
    right_index=True,
    direction='forward'
)

               118.5     119.0     119.5     120.0     120.5           F
Expiry                                                                  
2018-01-12  0.050842  0.050842  0.050842  0.050842  0.050842  123.000000
2018-01-19  0.039526  0.039526  0.039526  0.039526  0.039526  123.000000
2018-01-26  0.039196  0.039196  0.039196  0.039196  0.039196  123.000000
2018-02-02  0.039991  0.039991  0.039991  0.039991  0.039991  123.000000
2018-02-23  0.040005  0.040005  0.040005  0.040005  0.040005  123.000000
2018-03-23  0.041025  0.041000  0.040872  0.040623  0.040398  122.609375

如果你想要'F'在前面:

pd.merge_asof(
    piv, df2[['F']],
    left_index=True,
    right_index=True,
    direction='forward'
).pipe(lambda d: d[['F']].join(d.drop('F', 1)))

                     F     118.5     119.0     119.5     120.0     120.5
Expiry                                                                  
2018-01-12  123.000000  0.050842  0.050842  0.050842  0.050842  0.050842
2018-01-19  123.000000  0.039526  0.039526  0.039526  0.039526  0.039526
2018-01-26  123.000000  0.039196  0.039196  0.039196  0.039196  0.039196
2018-02-02  123.000000  0.039991  0.039991  0.039991  0.039991  0.039991
2018-02-23  123.000000  0.040005  0.040005  0.040005  0.040005  0.040005
2018-03-23  122.609375  0.041025  0.041000  0.040872  0.040623  0.040398