ferrelwill
ferrelwill

Reputation: 821

How can I select values in one column base on a condition in another using python?

Is there a way to filter rows if the column2 has all zeroes 10 minutes ahead from the current value in columnn1. How can I do this while keeping datetime index?

2020-01-01 00:01:00     60      0
2020-01-01 00:02:00     70      0
2020-01-01 00:03:00     80      0
2020-01-01 00:04:00     70      0
2020-01-01 00:05:00     60      0
2020-01-01 00:06:00     60      0
2020-01-01 00:07:00     70      0
2020-01-01 00:08:00     80      0
2020-01-01 00:09:00     80      2
2020-01-01 00:10:00     80      0
2020-01-01 00:11:00     70      0
2020-01-01 00:12:00     70      0
2020-01-01 00:13:00     50      0
2020-01-01 00:14:00     50      0
2020-01-01 00:15:00     60      0
2020-01-01 00:16:00     60      0
2020-01-01 00:17:00     70      0
2020-01-01 00:18:00     70      0
2020-01-01 00:19:00     80      0
2020-01-01 00:20:00     80      0
2020-01-01 00:21:00     80      1
2020-01-01 00:22:00     90      2

Expected output

2020-01-01 00:19:00     80      0
2020-01-01 00:20:00     80      0

Upvotes: 0

Views: 55

Answers (3)

ferrelwill
ferrelwill

Reputation: 821

I figured it out. It's actually simple.

input['col3'] = input['col2'].rolling(10).sum()

output = input.loc[(input['col3'] == 0)]

Upvotes: 1

4.Pi.n
4.Pi.n

Reputation: 1151

Using pandas.DataFrame.query, pandas.DataFrame.query - documentation

df.query(f'column_1 == {0} and column_2 == {value} or column_3 == {another_value}')

Upvotes: 0

BitLauncher
BitLauncher

Reputation: 693

Just a guess, because I do not know pandas, but assuming it is a bit like SQL or linq or linkable datasets in C# - what about linking/joining your table (A) with itself (B) for all 12 minutes, grouping by each row of A and then summing the column2 of B (if only positive values there) and filter (SQL having) by the ones whose sum is 0?

As result report A.column0, A.column1 and SUM(B.column2)?

Upvotes: 0

Related Questions