我的数据文件具有不断增加的值,但是当服务器重新启动时,这个系列就会被重置。
例如
值= {10,25,100,2,12,35,5,16}
我需要找出局部最大值和最小值之差之和。
上一组的答案是(100-10) + (35-2) + (16-5) = 90 + 33 + 11 = 134。
我怎样才能在熊猫中做到这一点呢?感谢你的帮助
发布于 2020-03-11 21:25:41
两步过程。
首先获取每个增量的第一个和最后一个数字:
df2 = df.groupby((df['col1'].shift()>df['col1']).cumsum()).nth([0,-1])然后得到每个组的差异,然后求和:
df2.groupby(level = 0).diff().sum()
0 134.0发布于 2020-03-11 21:43:50
您可以使用scipy.signal.argrelextrema:
from scipy.signal import argrelextrema
import numpy as np
value =np.array( [10, 25, 100, 2, 12, 35, 5, 16] )
max_=value[argrelextrema(value, np.greater)]
min_=value[argrelextrema(value, np.less)]
res=sum(max_)-sum(min_)
#just to include first el:
if(value[0]>value[1]): res+=value[0]
elif(value[0]<value[1]): res-=value[0]
#and the last one:
if(value[-1]>value[-2]): res+=value[-1]
elif(value[-1]<value[-2]): res-=value[-1]
print(res) #134https://stackoverflow.com/questions/60642709
复制相似问题