Home > Enterprise >  How to select every n-th row in dataframe with condition of previous rows based on daily interval
How to select every n-th row in dataframe with condition of previous rows based on daily interval

Time:11-27

I have a large dataframe and I need a new column sig with values 0 or 1.
The conditions:
Add value = 1 in 3rd row of each day starting at 08:30, if data in row 3 > data row 2 > data row 1, else 0
Limitations: In the original dataframe the intervals of the seconds in the timestamps are not equal, so you can't go by time intervals. The amount of rows per day varies.

Sample dataframe ( I don't know how to randomize seconds, so the intervals here are equal, also the amount of rows are equal):

import pandas as pd
import numpy as np

pd.set_option('display.max_rows', 500)
np.random.seed(100)
dates = pd.date_range("2022.01.01", "2022.01.31", freq="s")
dates=dates[:-1]
df = pd.DataFrame({'date':dates,
                   'data':np.random.randint(low=0, high=100, size=len(dates)).tolist()})
df['_date'] = pd.to_datetime(df['date'])
df.set_index('date', inplace=True)   
df = df.loc[(df._date.dt.hour == 8) &  (df._date.dt.minute == 30) &  ((df._date.dt.second >= 0) & (df._date.dt.second <= 10))].head(30)
df.drop(['_date'], axis=1, inplace=True)



                   data
date    
2022-01-01 08:30:00 14
2022-01-01 08:30:01 27
2022-01-01 08:30:02 33
2022-01-01 08:30:03 77
2022-01-01 08:30:04 66
2022-01-01 08:30:05 60
2022-01-01 08:30:06 72
2022-01-01 08:30:07 21
2022-01-01 08:30:08 70
2022-01-01 08:30:09 60
2022-01-01 08:30:10 76
2022-01-02 08:30:00 13
2022-01-02 08:30:01 73
2022-01-02 08:30:02 71
2022-01-02 08:30:03 78
2022-01-02 08:30:04 50
2022-01-02 08:30:05 80
2022-01-02 08:30:06 48
2022-01-02 08:30:07 24
2022-01-02 08:30:08 29
2022-01-02 08:30:09 43
2022-01-02 08:30:10 75
2022-01-03 08:30:00 11
2022-01-03 08:30:01 52

How to accomplish this?

Desired outcome:

                   data  sig
date    
2022-01-01 08:30:00 14   0
2022-01-01 08:30:01 27   0
2022-01-01 08:30:02 33   1
2022-01-01 08:30:03 77   0
2022-01-01 08:30:04 66   0
2022-01-01 08:30:05 60   0
2022-01-01 08:30:06 72   0
2022-01-01 08:30:07 21   0
2022-01-01 08:30:08 70   0
2022-01-01 08:30:09 60   0
2022-01-01 08:30:10 76   0
2022-01-02 08:30:00 13   0
2022-01-02 08:30:01 73   0
2022-01-02 08:30:02 71   0
2022-01-02 08:30:03 78   0
2022-01-02 08:30:04 50   0
2022-01-02 08:30:05 80   0
2022-01-02 08:30:06 48   0
2022-01-02 08:30:07 24   0
2022-01-02 08:30:08 29   0
2022-01-02 08:30:09 43   0
2022-01-02 08:30:10 75   0
2022-01-03 08:30:00 11   0
2022-01-03 08:30:01 32   0
2022-01-03 08:30:02 52   1
2022-01-03 08:30:03 44   0
2022-01-03 08:30:03 75   0

CodePudding user response:

I took your code to create the input data, but it looks bit different to the printed version of yours:

                     data
date                     
2022-01-01 08:30:00    14
2022-01-01 08:30:01    27
2022-01-01 08:30:02    33
2022-01-01 08:30:03    77
2022-01-01 08:30:04    66
2022-01-01 08:30:05    60
2022-01-01 08:30:06    72
2022-01-01 08:30:07    21
2022-01-01 08:30:08    70
2022-01-01 08:30:09    60
2022-01-01 08:30:10    76
2022-01-02 08:30:00    13
2022-01-02 08:30:01    73
2022-01-02 08:30:02    71
2022-01-02 08:30:03    78
2022-01-02 08:30:04    50
2022-01-02 08:30:05    80
2022-01-02 08:30:06    48
2022-01-02 08:30:07    24
2022-01-02 08:30:08    29
2022-01-02 08:30:09    43
2022-01-02 08:30:10    75
2022-01-03 08:30:00    11
2022-01-03 08:30:01    52
2022-01-03 08:30:02    40
2022-01-03 08:30:03    30
2022-01-03 08:30:04    44
2022-01-03 08:30:05    71
2022-01-03 08:30:06    64
2022-01-03 08:30:07    60

Your rules could be described as well as a rolling window of 3 rows, check if the window is already sorted (value3 bigger than 2 bigger than 1). Knowing that we could use this condition on the whole data (without paying attention to date) and create a Series with values of 1 if condition is True and 0 for False (named cond) Then search for the 3rd value of each day and map the value of that index in cond to the new column.

def window_sorted(grp):
    return (np.diff(grp) > 0).all()

cond = df['data'].rolling(window=3, min_periods=1).apply(window_sorted)
df['sig'] = 0
grp = df.groupby(pd.Grouper(level=0, freq='D'), as_index=False)['data'].nth(2).index
df.loc[grp, 'sig'] = cond[grp]
print(df)

Output:

                     data  sig
date                          
2022-01-01 08:30:00    14    0
2022-01-01 08:30:01    27    0
2022-01-01 08:30:02    33    1
2022-01-01 08:30:03    77    0
2022-01-01 08:30:04    66    0
2022-01-01 08:30:05    60    0
2022-01-01 08:30:06    72    0
2022-01-01 08:30:07    21    0
2022-01-01 08:30:08    70    0
2022-01-01 08:30:09    60    0
2022-01-01 08:30:10    76    0
2022-01-02 08:30:00    13    0
2022-01-02 08:30:01    73    0
2022-01-02 08:30:02    71    0
2022-01-02 08:30:03    78    0
2022-01-02 08:30:04    50    0
2022-01-02 08:30:05    80    0
2022-01-02 08:30:06    48    0
2022-01-02 08:30:07    24    0
2022-01-02 08:30:08    29    0
2022-01-02 08:30:09    43    0
2022-01-02 08:30:10    75    0
2022-01-03 08:30:00    11    0
2022-01-03 08:30:01    52    0
2022-01-03 08:30:02    40    0
2022-01-03 08:30:03    30    0
2022-01-03 08:30:04    44    0
2022-01-03 08:30:05    71    0
2022-01-03 08:30:06    64    0
2022-01-03 08:30:07    60    0
  • Related