I have the pandas DataFrame:
df = pd.DataFrame({
"computer": [0, 1, 2, 3, 0, 1, 2, 3],
"time": [0, 0, 1, 1, 2, 3, 4, 5],
"value": [4, 1, 5, 6, 1, 0, 3, 4],
})
df["time"] = pd.to_datetime(df["time"], unit="s")
df.set_index(["computer", "time"])
I would like to bin time
column into 4s intervals and sum the value
s.
The output would look like:
df = pd.DataFrame({
"computer": [0, 1, 2, 3, 2, 3],
"start_time": [0, 0, 0, 0, 4, 4],
"value": [5, 1, 5, 6, 3, 4],
})
df["start_time"] = pd.to_datetime(df["start_time"], unit="s")
df.set_index(["computer", "start_time"])
I've tried to apply the code provided by Pandas group by time windows, but I'm unable to get it working due to the multiindex.
I've also tried the resample
method, but the same problem applies.