如何找到组内值之间的最小差异

2024-06-26 09:44:35 发布

您现在位置:Python中文网/ 问答频道 /正文

示例DF

df = pd.DataFrame({'Animal': ['Falcon', 'Falcon', 'Falcon',
                              'Parrot', 'Parrot', 'Parrot'],
                   'Max Speed': [380., 370., 25., 24., 26., 29.]})

期望输出

2 (because the smallest difference is between 24 and 26 of Parrot)

更好(这样我可以检查)

'Parrot, 2'

我在想什么

“我需要按动物和最大速度排序,然后是一个窗口函数,但必须有更好的解决方案……”