我有2列,我想要diff列作为输出。我尝试使用循环迭代。如果我传递值数组,我想要diff列数组。
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
有人可以帮助我。
答案 0 :(得分:3)
IIUC,您需要cummax
:
df['diff'] = df['h'] - df['l']
df['diff'] = df['diff'].cummax()
您可以在一行中执行此操作:
df['diff'] = (df['h'] - df['l']).cummax()
输出:
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