Thursday, 17 January 2019

Pandas - get index of the first previous smaller value

I have a dataframe that looks like this:

index value
0     1
1     1
2     2
3     3
4     2
5     1
6     1

what I want is for each value to return the index of the previous smaller value, and, in addition, the index of the previous "1" value. If the value is 1 I don't need them (both values can be -1 or something).

So what I'm after is:

index value  previous_smaller_index  previous_1_index
0     1            -1                      -1
1     1            -1                      -1
2     2             1                       1
3     3             2                       1
4     2             1                       1
5     1            -1                      -1
6     1            -1                      -1

I tried using rolling, cumulative functions etc. but I couldn't figure it out. Any help would be appreciated!

Edit: SpghttCd already provided a nice solution for the "previous 1" problem. I'm looking for a nice pandas one liner for the "previous small" problem. (even though, of course, more nice and efficient solutions are welcomed for both problems)



from Pandas - get index of the first previous smaller value

No comments:

Post a Comment