I'm trying to sum all the value
where date1
matches date2
for each row in my dataframe, which is similar to this question.
id value date1 date2 sum
A 150 4/8/2014 3/8/2014 nan
B 100 5/8/2014 2/8/2014 nan
B 200 7/8/2014 5/8/2014 100
A 200 4/8/2014 3/8/2014 nan
A 300 6/8/2014 4/8/2014 350
I've tried the following but keep getting 'Lengths must match to compare' error.
grp = df.groupby('id')
df['sum'] = grp.apply(lambda x: x[x['date1'] == df['date2'].values]['value'].sum())
Would appreciate any advice!