Reputation: 113
I want to create a list with dates when the following happens to the data in Python.
I gave a sample dataset in the table below. However, this happens multiple times and that is where I am struggling. I tried a for-loop to create the data but it only gives back the first time that this occurs, while I am interested in all events.
Sample data set:
Date and Time | Process value |
---|---|
2020-06-07 00:00 | 8.2 |
2020-06-07 01:00 | 6.5 |
2020-06-07 02:00 | 4.3 |
2020-06-07 03:00 | 3.6 |
2020-06-07 04:00 | 0 |
2020-06-07 05:00 | 0 |
2020-06-07 06:00 | 0 |
2020-06-07 07:00 | 3.2 |
2020-06-07 08:00 | 4.8 |
2020-06-07 09:00 | 0 |
Expected result:
Date and Time | Process value |
---|---|
2020-06-07 03:00 | 3.6 |
2020-06-07 04:00 | 0 |
2020-06-07 08:00 | 4.8 |
2020-06-07 09:00 | 0 |
Any tips?
Upvotes: 1
Views: 130
Reputation: 2684
Fun way to do this one:
Setup:
import pandas as pd
data = {
"Date and Time": ["2020-06-07 00:00", "2020-06-07 01:00", "2020-06-07 02:00", "2020-06-07 03:00",
"2020-06-07 04:00", "2020-06-07 05:00", "2020-06-07 06:00", "2020-06-07 07:00",
"2020-06-07 08:00", "2020-06-07 09:00"],
"Process value": [8.2, 6.5, 4.3, 3.6, 0, 0, 0, 3.2, 4.8, 0]
}
df = pd.DataFrame(data)
Shift to find previous values:
below_4 = (df["Process value"] < 4) & (df["Process value"].shift(1) > 4)
above_4 = (df["Process value"] > 4) & (df["Process value"].shift(1) < 4)
reach_0 = (df["Process value"] == 0) & (df["Process value"].shift(1) != 0)
df.loc[below_4 | above_4 | reach_0]
Yields:
Date and Time Process value
3 2020-06-07 03:00 3.6
4 2020-06-07 04:00 0.0
8 2020-06-07 08:00 4.8
9 2020-06-07 09:00 0.0
Upvotes: 0
Reputation: 12558
You need to remember state.
result = []
last_event = "init"
for item in items:
if last_event == "init" and item["value"] < 4.0:
result.append(item)
last_event = "below_4"
elif last_event == "below_4" and item["value"] == 0:
result.append(item)
last_event = "is_0"
elif last_event == "is_0" and item["value"] > 4.0:
result.append(item)
last_event = "above_4"
Normally, you'd define the possible states separately in a dict or dataclass, so that new states are not randomly introduced across a larger program.
Upvotes: 1