【发布时间】:2021-06-21 10:50:00
【问题描述】:
我正在尝试通过日期时间索引覆盖产品 df 中预先计算的权重。我的挑战是只覆盖到某个日期(在另一个 df 中列出),然后再做一次。数据示例:
data = {'Product 1 Weight':['0', '.15', '.19', '.2','.21','.25','.252','.255'],
'Product 2 Weight':['0', '0', '0', '0','0','0','0','0'],
'Product 3 Weight':['0', '0', '0', '0','0','.5','.551','.561']}
df = pd.DataFrame(data, index =['2020-04-01',
'2020-04-02',
'2020-04-03',
'2020-04-06',
'2020-04-07',
'2020-04-08',
'2020-04-09',
'2020-04-10'])
rebalances= pd.DataFrame({'Rebalance':['2020-04-02',
'2020-04-08',
'2020-04-10']})
在此示例中,我想用 2020-04-02 的值覆盖从 2020-04-02 到 2020-04-07 的所有产品的值。然后我想用 2020-04-08 的值覆盖从 2020-04-08 到 2020-04-09 的所有产品的值,依此类推。重新平衡 df 将为我提供停止覆盖并开始另一个覆盖的日期。因此,我想要的最终输出看起来像:
data = {'Product 1 Weight':['0', '.15', '.15', '.15','.15','.25','.25','.255'],
'Product 2 Weight':['0', '0', '0', '0','0','0','0','0'],
'Product 3 Weight':['0', '0', '0', '0','0','.5','.5','.561']}
df = pd.DataFrame(data, index =['2020-04-01',
'2020-04-02',
'2020-04-03',
'2020-04-06',
'2020-04-07',
'2020-04-08',
'2020-04-09',
'2020-04-10'])
可能看起来完全随机,但对我当前的项目来说会很棒。
【问题讨论】:
标签: python pandas dataframe overwrite