Skip to content
Advertisement

Getting max values based on sliced column

Let’s consider this Dataframe:

JavaScript

I want to compute column D based on:

  • The max value between:
    • integer 0
    • A slice of column B at that row’s index

So I have created a column C (all zeroes) in my dataframe in order use DataFrame.max(axis=1). However, short of using apply or looping over the DataFrame, I don’t know how to slice the input values. Expected result would be:

JavaScript

So essentially, d’s 3rd row is computed (pseudo-code) as max(df[3:,"b"], df[3:,"c"]), and similarly for each row.

Since the input columns (b, c) have already been computed, there has to be a way to slice the input as I calculate each row for D without having to loop, as this is slow.

Advertisement

Answer

Seems like this could work: Reverse “B”, find cummax, then reverse it back and assign it to “d”. Then use where on “d” to see if any value is less than 0:

JavaScript

We can replace the last line with the one below using clip (thanks @Either), and drop the 2nd reversal (assuming indexes match) making it all a one liner:

JavaScript

Output:

JavaScript
Advertisement