I need to find if the last value of dataframe['position'] is different from 0, then count the previous (so in reverse) values until them changes and store the counted index before the change in a variable, this without for loops. By loc or iloc for example...
dataframe: | position | 0 1 1 0 2 1 <4 3 1 <3 4 1 <2 5 1 <1 count = 4 I achieved this by a for loop, but I need to avoid it:
count = 1 if data['position'].iloc[-1] != 0: for i in data['position']: if data['position'].iloc[-count] == data['position'].iloc[-1]: count = count + 1 else: break if data['position'].iloc[-count] != data['position'].iloc[-1]: count = count - 1
df['col1'][::-1].cumprod().sum()