Pandas groupby and sum according to an index date from two data frames
Solution 1:
Use DataFrame.merge
with filtering by boolean indexing
with Series.between
and then aggregate sum
:
df2 = df1.merge(df, on='MemberID', how='outer')
df2 = df1.merge(df, on='MemberID', how='outer')
df2 = (df2[df2['serviceDate'].between(df2['lookupDate'], df2['indexDate'])]
.groupby(['MemberID','indexDate','lookupDate','yValue'], as_index=False).sum())
df2['yValue'] = df2.pop('yValue')
print (df2)
MemberID indexDate lookupDate feature1 feature2 feature3 yValue
0 a 2020-02-03 2019-02-03 0 1 1 9
1 a 2020-11-02 2019-11-02 2 1 0 2