Python: Lambda function with multiple conditions based on multiple previous rows

Viewed 201

I am trying to define a lambda function that assigns True or False to a row based on various conditions. There is a column with a Timestamp and what I want is, that if within the last 10 seconds (based on the timestamp of the current row x) some specific values occured in other columns of the dataset, the current row x gets the True or False tag.

So basically I have to check whether in the previous n rows, i.e. Timestamp(x) - 10 seconds value a occured in column A and value b occured in column B.

I already looked at the shift() function with freq = 10 seconds and another attempt looked like that:

data['Timestamp'][(data['Timestamp']-pd.Timedelta(seconds=10)):data['Timestamp']]

But I wasn't able to proceed with either of the two options.

Is it possible to start an additional select within a lambda function? If yes, how could that look like?

P.S.: Working with regular for-loops instead of the lambda function is not an option due to the overall setup of the application/code.

Thanks for your help and input!

1 Answers

Perhaps you're looking for something like this, if I understood correctly:

def create_tag(current_timestamp, df, cols_vals):
    # Before the current timestamp
    mask = (df['Timestamp'] <= current_timestamp)
    # After the current timestamp - 10s
    mask = mask & (df['Timestamp'] >= current_timestamp - pd.to_timedelta('10s'))
    # Filter all dataframe following the mask
    filtered = df[mask]
    # Check if each val of col is present
    present = all(value in filtered[column_name].values for column_name, value in cols_vals.items())
    return present
        
data['Tag'] = data['Timestamp'].apply(lambda x: create_tag(x, data, {'column A': 'a', 'column B', 'b'}))

The idea behind this code is, for each timestamp that you have, we're going to apply the create_tag function. This takes the current timestamp, the whole dataframe as well as a dictionary containing column names as keys and the respective values you're looking for as values.

Related