Skip to content Skip to sidebar Skip to footer

Check Pandas Column For Successive Row Values

I have: hi 0 1 1 2 2 4 3 8 4 3 5 3 6 2 7 8 8 3 9 5 10 4 I have a list of lists and singl

Solution 1:

You can do it with np.logical_and.reduce + shift. But there are a lot of edge cases to deal with:

import numpy as np

def find_idx(seq, df, col):
    if type(seq) != list:     # if not list
        s = df[col].eq(seq)
        if s.sum() >= 1:      # if something matched
            idx = s.idxmax().item()
        else:
            idx = np.NaN
    elif seq:                 # if a list that isn't empty
        seq = seq[::-1]       # to get last index
        m = np.logical_and.reduce([df[col].shift(i).eq(seq[i]) for i in range(len(seq))])
        s = df.loc[m]
        if not s.empty:       # if something matched
            idx = s.index[0]
        else:
            idx = np.NaN
    else:                     # empty list
        idx = np.NaN
    return idx

l = [[2,8,3], 2, [2,8]]
[find_idx(seq, df, col='hi') for seq in l]
#[8, 1, 7]

l = [[2,8,3], 2, [2,8], [], ['foo'], 'foo', [1,2,4,8,3,3]]
[find_idx(seq, df, col='hi') for seq in l]
#[8, 1, 7, nan, nan, nan, 5]

Post a Comment for "Check Pandas Column For Successive Row Values"