Pratham
Pratham

Reputation: 101

return dynamic value from newly generated column value

I have 2 columns and i want diff column as an output.I tried with loops iteration.if i am passing array of values and i want diff column array.

 l                h              diff           
 100.87           100.87         max(h-l)

99.800778         100.87         max ((h-l),diff[0])

101.1281283     101.1281283      max ((h-l),diff[1])     

106.4575807     106.4575807      max ((h-l),diff[2])

109.3212896     109.3212896        .....

107.7907916     109.3212896

105.128359      109.3212896

103.8668187     109.3212896

108.9978396     109.3212896

110.0006197     110.0006197

Can someone help me out.

Upvotes: 0

Views: 36

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

IIUC, you need cummax:

df['diff'] = df['h'] - df['l']
df['diff'] = df['diff'].cummax()

You can do this in one line:

df['diff'] = (df['h'] - df['l']).cummax()

Output:

            l           h      diff
0  100.870000  100.870000  0.000000
1   99.800778  100.870000  1.069222
2  101.128128  101.128128  1.069222
3  106.457581  106.457581  1.069222
4  109.321290  109.321290  1.069222
5  107.790792  109.321290  1.530498
6  105.128359  109.321290  4.192931
7  103.866819  109.321290  5.454471
8  108.997840  109.321290  5.454471
9  110.000620  110.000620  5.454471

Upvotes: 3

Related Questions