Say I have the following DataFrame:
df = pd.DataFrame({'a': [12, 34, -45], 'b':[-24, 36, 48], 'c':[28, -14, 68]})
df:
a b c
12 -24 28
34 36 -14
-45 48 68
I am looking to return the index(+1) of the first column to contain a negative number within each row, so for the example I would produce:
a b c first_neg_col
12 -24 28 2
34 36 -14 3
-45 48 68 1
I have ways of achieving this:
def first_negval(val_list):
for idx, val in enumerate(val_list):
if val < 0:
return idx + 1
df['first_neg_col'] = df[:].values.tolist()
df.first_neg_col= df['first_neg_col'].apply(lambda x: first_negbal(x))
But this seems cumbersome/inefficient. I was wondering if there was a more vectorized approach / some way of using list comprehension?