Skip to content
Advertisement

If value is greater than the previous replace with previous in Pandas

I am working on a data processing script that does some basic calcs from data files and sorts the data. The last piece to the puzzle is identifying if the next value is > the previous and if it is replace the next value with the previous.

My df is set up as this:

JavaScript

I want the program to identify that the 6600 value is greater than the 6550. Once identified it should replace that 6600 with 6550 and continue over the entire df. I have done something similar, but it was for specific values like NAN or zeros. Anyways below is what I need the df to be transformed to:

JavaScript

Any help would be appreciated!

Advertisement

Answer

I was able to solve my question pretty simply.

JavaScript

Which outputs:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement