Home > Software engineering >  How to compare with the previous line after reassignment
How to compare with the previous line after reassignment

Time:07-01

Compare each row of column A with the previous row If greater than, reassign to the value of the previous row If less than, the value is unchanged Now the problem is that each time the comparison is made with the original value What I want is, to compare with the previous line after reassignment

import pandas as pd
import numpy as np
d={'A':[16,19,18,15,13,16]}
df = pd.DataFrame(d)
df['A_changed']=np.where(df.A>df.A.shift(),df.A.shift(),df.A)
df
    A   A_changed
0   16  16.0
1   19  16.0
2   18  18.0
3   15  15.0
4   13  13.0
5   16  13.0

expected output

 A   A_changed
0   16  16.0
1   19  16.0
2   18  16.0
3   15  15.0
4   13  13.0
5   16  13.0

CodePudding user response:

Are you trying to do cummin?

df['compare_min'] = df['A'].cummin()

Output:

    A  compare  compare_min
0   5      5.0            5
1  14      5.0            5
2  12     12.0            5
3  15     12.0            5
4  13     13.0            5
5  16     13.0            5

df['b'] = [10, 11, 12, 5, 8, 2]

df['compare_min_b'] = df['b'].cummin()

Output:

    A  compare  compare_min   b  compare_min_b
0   5      5.0            5  10             10
1  14      5.0            5  11             10
2  12     12.0            5  12             10
3  15     12.0            5   5              5
4  13     13.0            5   8              5
5  16     13.0            5   2              2

Update using your example, this exactly what cummin does:

d={'A':[16,19,18,15,13,16]}
df = pd.DataFrame(d)
df['A_change'] = df['A'].cummin()
df

Output:

    A  A_changed  A_change
0  16       16.0        16
1  19       16.0        16
2  18       18.0        16
3  15       15.0        15
4  13       13.0        13
5  16       13.0        13

Here is why your code will not work.

d={'A':[16,19,18,15,13,16]}
df = pd.DataFrame(d)

df['A_shift'] = df['A'].shift()
df

Output:

    A  A_shift
0  16      NaN
1  19     16.0
2  18     19.0
3  15     18.0
4  13     15.0
5  16     13.0

Look at the output of the shifted column, what you want to do is keep the culumative mine instead of just comparing A to shifted A. Hence index 2 is not giving you what you expected.

  • Related