'Pandas: Mean of a column between change of condition in second column
Say I have the following dataframe:
import pandas as pd
import numpy as np
data = np.random.randint(1, 10, size=(10,2))
df = pd.DataFrame(data, columns=['x1', 'x2'])
df['switch'] = [1,1,0,0,1,1,0,0,1,1]
index_ = pd.date_range('2022-01-17 13:00:00', periods=10, freq='5s')
df.index = index_.rename('Time')
resulting in:
x1 x2 switch
Time
2022-01-17 13:00:00 2 6 1
2022-01-17 13:00:05 9 8 1
2022-01-17 13:00:10 4 9 0
2022-01-17 13:00:15 5 6 0
2022-01-17 13:00:20 4 9 1
2022-01-17 13:00:25 6 7 1
2022-01-17 13:00:30 4 6 0
2022-01-17 13:00:35 2 3 0
2022-01-17 13:00:40 4 9 1
2022-01-17 13:00:45 5 2 1
I'm looking for a way of getting the start time, end time, and means of x1 & x2 for each block where the switch value is 1.
So here (for example):
Start: 2022-01-17 13:00:00
End: 2022-01-17 13:00:05
x1 mean: 5.5
x2 mean: 7
I have no idea how to both detect the change in the switch column and then also to build the mean from the values occurring between the change and return the times at which the change in the switch column occurred.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
