'How do I find last index of true value in a dataframe when applying condition to each row in an efficient way in python?

Let us say I have pandas dataframe having two columns, previous and current. We can assume that values are non-decreasing and current values are always greater than previous value.

Now, for each element in previous column, I want to look up index of last value of current column which is less than this value. I then want to subtract that index from the this element's index and store that value in the new column, say numIndexes

working but inefficient code is as follows:

df = pd.DataFrame({'previous': [1,3,5,7,9,11,13,17],'current': [2,6,9,10,15,19,20,21]})
df['numIndexes']=1
for i in range(len(df)):
    x=df['previous'][i]>df['current']
    df['numIndexes'][i]=i-x[::-1].idxmax()

OUTPUT
    previous    current numIndexes
0   1   2   -7
1   3   6   1
2   5   9   2
3   7   10  2
4   9   15  3
5   11  19  2
6   13  20  3
7   17  21  3

Ignore the first negative value.

To explain my objective via example above, for 5th index, we have previous value as 11. Now in the current column, last index where current value is less than 11 is index 3. This gives me numIndexes for 5th row as 2 ( 5-3)

For a large dataset, this method is extremely slow. Any help appreciated to speed up this logic.

EDIT : The assumption of strictly increasing values is not correct. Values are non-decreasing. However, each previous value is strictly less than its corresponding current value



Solution 1:[1]

I am doing the subtract.outer, with argmax

df.index - np.cumsum(np.subtract.outer(df['previous'].values,df['current'].values),axis=1).argmax(axis=1)
Out[278]: Int64Index([0, 1, 2, 2, 3, 2, 3, 3], dtype='int64')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BENY