Use a bit changed this solution - for testing use in
operator - it loop only to matching like your requirement:
from numba import njit
@njit
def get_first_index_nb(A, k):
for i in range(len(A)):
if A[i] in k:
return i
return None
#pandas 0.24+
idx = get_first_index_nb(df.track.to_numpy(), ['V1', 'V2'])
#oldier pandas versions
#idx = get_first_index_nb(df.track.values, ['V1', 'V2'])
print (idx)
Solution with Series.idxmax
if possible no values matching with if-else
statement and Series.any
, but it test all matching values:
m = df.track.isin(['V1', 'V2'])
idx = m.idxmax() if m.any() else None
Or:
idx = next(iter(df.index[df.track.isin(['V1', 'V2'])]), None)