I have a dataframe df
with a very long column of random positive integers:
df = pd.DataFrame({'n': np.random.randint(1, 10, size = 10000)})
I want to determine the index of the first even number in the column. One way to do this is:
df[df.n % 2 == 0].iloc[0]
but this involves a lot of operations (generate the indices f.n % 2 == 0
, evaluate df
on those indices and finally take the first item) and is very slow. A loop like this is much quicker:
for j in range(len(df)):
if df.n.iloc[j] % 2 == 0:
break
also because the first result will be probably in the first few lines. Is there any pandas method for doing this with similar performance? Thank you.
NOTE: This condition (to be an even number) is just an example. I'm looking for a solution that works for any kind of condition on the values, i.e., for a fast one-line alternative to:
df[ conditions on df.n ].iloc[0]