-2

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:

Date Minimum Z 2020-06-30 6550 2020-07-31 6600 2020-08-31 6550 2020-09-30 6540 2020-10-31 6530 

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:

 Date Minimum Z 2020-06-30 6550 2020-07-31 6550 2020-08-31 6550 2020-09-30 6540 2020-10-31 6530 

Any help would be appreciated!

0

2 Answers 2

1

You can shift the column, then compare each row with the previous value:

>>> pd.concat([df['Minimum Z'], df['Minimum Z'].shift()], axis=1) Minimum Z Minimum Z 0 6550 NaN 1 6600 6550.0 2 6550 6600.0 3 6540 6550.0 4 6530 6540.0 

Now it seems that all you need to do is take the minimum of each row:

>>> pd.concat([df['Minimum Z'], df['Minimum Z'].shift()], axis=1).min(axis=1) 0 6550.0 1 6550.0 2 6550.0 3 6540.0 4 6530.0 dtype: float64 
Sign up to request clarification or add additional context in comments.

4 Comments

Wow it took a minute to wrap my head around that, but then it hit me. Really clever.
Strange just double checked this and its not working as described.
What exactly doesn't work?
Sorry I did not reply sooner I was in a deadzone all weekend. Anyways, the above code for me only compared the values in the same row. The shift in this case doesn't input the previous lower value for some reason.
1

I was able to solve my question pretty simply.

df['min z'] = df5['Minimum Z'].expanding().apply(min) df = df5.drop(columns=['Minimum Z']) 

Which outputs:

 Date min z 0 2020-06-30 6550.000000 1 2020-07-31 6550.000000 2 2020-08-31 6550.000000 3 2020-09-30 6540.577164 4 2020-10-31 6530.831152 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.