【发布时间】:2021-02-13 11:27:06
【问题描述】:
df1
trade_date pct_chg
266 2019-09-20 0.2390
265 2019-09-23 -0.9769
264 2019-09-24 0.2776
263 2019-09-25 -1.0018
262 2019-09-26 -0.8914
261 2019-09-27 0.1182
260 2019-09-30 -0.9201
259 2019-10-08 0.2885
258 2019-10-09 0.3874
257 2019-10-10 0.7814
df2
obs_date last_recent_day_pct_chg last_second_day_pct_chg avg_l3d_pct_chg max_l3d_pct_chg
a 2019/9/21
b 2019/9/22
c 2019/9/23
d 2019/9/24
e 2019/9/25
f 2019/9/26
g 2019/9/27
h 2019/9/28
i 2019/9/29
j 2019/9/30
k 2019/10/5
l 2019/10/8
m 2019/10/9
n 2019/10/9
o 2019/9/29
注意:df2 原来只有 index 和 obs_date。
目标
我想根据df1 获取df2 列的值,如下所示:
- last_recent_day_pct_chg : 如果
obs_date可以在trade_date中找到,则返回对应的 pct_chg.(例如 2019/9/23:-0.9769)。如果没有,则返回对应的 last_recent_day pct_chg。(例如 2019/9/21: 0.2390 , 2019/10/5:-0.9201)。 - last_second_day_pct_chg : 如果
obs_date可以在trade_date中找到,则返回对应的最后一天pct_chg。(例如2019/9/24:0.2776)。如果没有,它将返回最近对应的last_second_day_pct_chg。(例如2019/ 10/5:0.1182,2019/9/23:空)。 - avg_l3d_pct_chg/max_l3d_pct_chg:基于
obs_date,返回最近3个trade_date的pct_chg的平均值或最大值。(例如2019/10/5、2019/9/26、9/27、9/30 , 2019/10/09, 2019/10/09,10/8, 9/30)
试试
我考虑shift method 和这个post。但它似乎不起作用。
【问题讨论】:
标签: pandas