I have a pd.DataFrame that I would like to convert:
id values days time value_per_day
0 1 15 15 1 1
1 1 20 5 2 4
2 1 12 12 3 1
I would like to combine them into equal buckets in 10 days. Since daysat time 1 it is greater than 10, this should spill into the next line, having the value/daysecond line on average 1st and 2nd.
Here is the resulting output, where (values, 0) = 15*(10/15) = 10and (values, 1) = (5+20)/2:
id values days value_per_day
0 1 10 10 1.0
1 1 25 10 2.5
2 1 10 10 1.0
3 1 2 2 1.0
I tried pd.Grouper:
df.set_index('days').groupby([pd.Grouper(freq='10D', label='right'), 'id']).agg({'values': 'mean'})
Out[146]:
values
days id
5 days 1 16
15 days 1 10
But I am clearly using it incorrectly.
csv for convenience:
id,values,days,time
1,10,15,1
1,20,5,2
1,12,12,3
source
share