9I have a multi-indexed dataframe which I would like to resample to reduce the frequency of datapoints by a factor of 3 (meaning that every 3 rows become one).
This:
time value
ID measurement
ET001 0 0 2
1 0.15 3
2 0.3 4
3 0.45 3
4 0.6 3
5 0.75 2
6 0.9 3
ET002 0 0 2
1 0.16 5
2 0.32 4
3 0.45 3
4 0.6 3
5 0.75 2
I want to turn into this:
time value
ID measurement
ET001 0 0.15 3
1 0.6 2.7
2 0.9 3
ET002 0 0.16 3.7
1 0.6 2.7
I tried to turn my time column into a pandas datetime index like so, and then use resample:
df = df.set_index(pd.DatetimeIndex(timecourse_normed['Time']))
df = df.groupby(level=0).resample(rule='0.1S', how=np.mean)
But the first line of that gives me actual dates (1970-something) which is quite unhelpful for the second line. Browsing arund stack overflow I found some similar quiestios which all had solutions NOT based on panda's resample - and also, sadly, not viable for my use case.
Could you give me a hand?
I think the idea for you could be - divide records inside each
ID
into bins by 3 records each (like ntile(3) in SQL) group by it and calculate mean. To create this numbers we can use the fact that you already have sequential numbers for each row -measurement
level of index. So we can just divide this number by3
to get numbers we need:So we can use helper function like this and apply it to each group to get desired results.
Hope it helps.