Pandas:在具有条件的数据框中查找峰值元素

Pandas: Find the peak element in a Data frame with condition

提问人:Gopinathan 提问时间:6/13/2023 最后编辑:Gopinathan 更新时间:6/13/2023 访问量:60

问:

我有一组值为“A”、“B”的数据框,..在“ID”列中。 需要从“值”列中找到峰值元素值,其中“类型”等于低。匹配的条件值需要填充在不同的列(“Low_Peak_Value”和“Peak_Time”列)中,示例输出如下所示,任何人都可以建议如何做到这一点。 列“ID”具有各种值集;因此,此条件应适用于“ID”列中的每个组。

data = {'ID': ['A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A', 'A',
               'A'],
        'Date': ['2023-05-12 16:00:00', '2023-05-15 15:00:00', '2023-05-16 14:00:00', '2023-05-17 07:00:00',
                 '2023-05-17 20:00:00', '2023-05-18 02:00:00', '2023-05-18 16:00:00', '2023-05-19 07:00:00',
                 '2023-05-25 05:00:00',
                 '2023-05-26 01:00:00', '2023-05-26 16:00:00',
                 '2023-05-29 09:00:00', '2023-05-31 17:00:00', '2023-06-01 22:00:00', '2023-06-05 13:00:00',
                 '2023-06-06 03:00:00', '2023-06-06 15:00:00',
                 '2023-06-06 22:00:00', '2023-06-07 18:00:00', '2023-06-08 05:00:00', '2023-06-08 22:00:00',
                 '2023-06-09 11:00:00'],
        'Value': [89845, 90065, 89462, 89437, 89455, 89248, 89013, 89405, 88687, 88521, 88543, 88682, 87747, 88208,
                  88406, 88731, 89118, 89178, 88824, 88865, 89459, 89363],
        'Type': ['Low', '', '', 'Low', '', '', 'Low', '', '', 'Low', '', '', 'Low', '', 'Low', '', '', '', 'Low', '',
                 '', ''],
        'Low_Peak_Value': ['', 89845, 89845, 89845, 89845, 89845, 89845, 89845, 89845, 89845, 89845,
                           89845, 89845, 87747, 87747, 87747, 87747, 87747, 87747, 87747, 87747, 87747],
        'Peak_Time': ['', '2023-05-12 16:00:00', '2023-05-12 16:00:00', '2023-05-12 16:00:00', '2023-05-12 16:00:00',
                      '2023-05-12 16:00:00', '2023-05-12 16:00:00', '2023-05-12 16:00:00', '2023-05-12 16:00:00',
                      '2023-05-12 16:00:00', '2023-05-12 16:00:00', '2023-05-12 16:00:00', '2023-05-12 16:00:00',
                      '2023-05-31 17:00:00', '2023-05-31 17:00:00', '2023-05-31 17:00:00', '2023-05-31 17:00:00',
                      '2023-05-31 17:00:00', '2023-05-31 17:00:00', '2023-05-31 17:00:00', '2023-05-31 17:00:00',
                      '2023-05-31 17:00:00']
        }

df = pd.DataFrame(data)

print(df)

Available Data:
**************
ID           Date          Value    Type        
A   12-05-2023 16:00:00    89845    Low         
A   15-05-2023 15:00:00    90065        
A   16-05-2023 14:00:00    89462        
A   17-05-2023 07:00:00    89437    Low 
A   17-05-2023 20:00:00    89455        
A   18-05-2023 02:00:00    89248        
A   18-05-2023 16:00:00    89013    Low 
A   19-05-2023 07:00:00    89405        
A   25-05-2023 05:00:00    88687        
A   26-05-2023 01:00:00    88521    Low 
A   26-05-2023 16:00:00    88543        
A   29-05-2023 09:00:00    88682        
A   31-05-2023 17:00:00    87747    Low 
A   01-06-2023 22:00:00    88208        
A   05-06-2023 13:00:00    88406    Low 
A   06-06-2023 03:00:00    88731        
A   06-06-2023 15:00:00    89118        
A   06-06-2023 22:00:00    89178        
A   07-06-2023 18:00:00    88824    Low 
A   08-06-2023 05:00:00    88865        
A   08-06-2023 22:00:00    89459        
A   09-06-2023 11:00:00    89363        

Required Output:
***************
ID           Date       Value   Type    Low_Peak_Value  Peak_Date   
A   12-05-2023 16:00:00 89845   Low         
A   15-05-2023 15:00:00 90065          89845        12-05-2023 16:00:00
A   16-05-2023 14:00:00 89462          89845        12-05-2023 16:00:00
A   17-05-2023 07:00:00 89437   Low    89845        12-05-2023 16:00:00
A   17-05-2023 20:00:00 89455          89845        12-05-2023 16:00:00
A   18-05-2023 02:00:00 89248          89845        12-05-2023 16:00:00
A   18-05-2023 16:00:00 89013   Low    89845        12-05-2023 16:00:00
A   19-05-2023 07:00:00 89405          89845        12-05-2023 16:00:00
A   25-05-2023 05:00:00 88687          89845        12-05-2023 16:00:00
A   26-05-2023 01:00:00 88521   Low    89845        12-05-2023 16:00:00
A   26-05-2023 16:00:00 88543          89845        12-05-2023 16:00:00
A   29-05-2023 09:00:00 88682          89845        12-05-2023 16:00:00
A   31-05-2023 17:00:00 87747   Low    89845        12-05-2023 16:00:00
A   01-06-2023 22:00:00 88208          87747        31-05-2023 17:00:00
A   05-06-2023 13:00:00 88406   Low    87747        31-05-2023 17:00:00
A   06-06-2023 03:00:00 88731          87747        31-05-2023 17:00:00
A   06-06-2023 15:00:00 89118          87747        31-05-2023 17:00:00
A   06-06-2023 22:00:00 89178          87747        31-05-2023 17:00:00
A   07-06-2023 18:00:00 88824   Low    87747        31-05-2023 17:00:00
A   08-06-2023 05:00:00 88865          87747        31-05-2023 17:00:00
A   08-06-2023 22:00:00 89459          87747        31-05-2023 17:00:00
A   09-06-2023 11:00:00 89363          87747        31-05-2023 17:00:00
Python Pandas DataFrame 条件语句 匹配

评论

0赞 Stu Sztukowski 6/13/2023
您能描述一下该组与您想要的输出有何不同吗?8774789845
0赞 Gopinathan 6/13/2023
我需要找到“类型”列的值为低值的滚动峰值低值,因为这些是极值低值
0赞 Stu Sztukowski 6/13/2023
明白了。谢谢!
0赞 jqurious 6/13/2023
也许有人可以解释为什么峰值会变为 ? 是最大的低值(我认为这就是峰值的意思) - 但我不明白为什么它会变成 - 我唯一能看到的变化是月份,它每个月都会重置吗?877478984587747

答: 暂无答案