Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从新生成的列值返回动态值_Python_Pandas - Fatal编程技术网

Python 从新生成的列值返回动态值

Python 从新生成的列值返回动态值,python,pandas,Python,Pandas,我有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])

我有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

有人能帮我吗。

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

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

供将来参考:供将来参考:@Pratham此解决方案对您有帮助吗?你会考虑吗?“Pratham有这个解决方案吗?”你会考虑吗?