使用熊猫在图表上查找最大值

时间:2020-07-21 18:23:02

标签: python pandas

我想在图表上找到这些最大值: enter image description here

这是我的DataFrame。

dt_object       coordinate
01.01.2019 0:00     1.4601
01.01.2019 1:00     1.46051
01.01.2019 2:00     1.46092
01.01.2019 3:00     1.46133
01.01.2019 4:00     1.46173
01.01.2019 5:00     1.46214
01.01.2019 6:00     1.46254
01.01.2019 7:00     1.46294
01.01.2019 8:00     1.46334
01.01.2019 9:00     1.46374

搜索算法如下:

  1. 脚本从第一行到最后一行。

  2. 脚本使用当前行并查看坐标。

  3. 脚本需要10个上一行和10个下一行。

  4. 如果当前坐标是前10个值和后10个值之间的最大值,则为最大值。

我已经使用迭代实现了此任务。但是它工作非常缓慢。是否有可能利用熊猫的力量加快寻找最大值的过程?

df_work=df_work.assign(max=False)  # add new column 'max'. Default value is False
df_len = len (df_work)
for row in df_work.iterrows():
    index = row[0]
    value = row[1][1]
    if (index > 10) and index < (df_len - 10):
        # get previous and future values
        collect_neighbours = []
        for i in range (1, 10+1):
            prev_val = df_work['coordinate'][index - i]
            fut_val = df_work['coordinate'][index + i]
            collect_neighbours.append(prev_val)
            collect_neighbours.append (fut_val)
        max_in_neighbours = max(collect_neighbours)
        if value >= max_in_neighbours:
            print ('maximum is found!')
            df_work.at[index, 'max'] = True

0 个答案:

没有答案
相关问题