python - 如何从 Pandas 数据框中每 7 条记录具有最大值的数据框中获取整行?

标签 python pandas

例如。考虑下面给出的数据框:

    Timestamp  in_speed
   1625638530    268.78
   1625638590    262.75
   1625638650    265.43
   1625638710    270.67
   1625638770    261.13
   1625638830    265.49
   1625638890    266.51
   1625638950    270.54
   1625639010    275.12
   1625639070    267.62
   1625639130    267.20
   1625639190    265.29
   1625639250    261.95
   1625639310    264.39
   1625639370    270.76
   1625639430    291.18

我想提取每 7 行包含最大值的整行。因此,所需的输出将是:

  1625638710    270.67
  1625639010    275.12
  1625639430    291.18

最佳答案

使用DataFrameGroupBy.idxmax按最大值获取索引并按 DataFrame.loc 选择:

df = df.loc[df.groupby(df.index // 7)['in_speed'].idxmax()]
#alternative for not default index
#df = df.loc[df.groupby(np.arange(len(df)) // 7)['in_speed'].idxmax()]
print (df)
     Timestamp  in_speed
3   1625638710    270.67
8   1625639010    275.12
15  1625639430    291.18

关于python - 如何从 Pandas 数据框中每 7 条记录具有最大值的数据框中获取整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68284343/

相关文章:

python - 删除 celery 中的 Task/PeriodicTask

python - Pandas 中的字符串包含

sql - 具有多列的表中每组的第一个非空值

python - 将输入直接添加到列表

python - 根据其他列值编辑 pandas 数据框中的值

python - Pandas 数据框作为字符串连接连接/合并为单列,便宜

pandas - 基于子级别的分层索引

python - Pandas groupby 根据列值和组大小份额选择前 N 行

python - 有没有办法在 python matplotlib 中设置特定子图的背景颜色?

python - 与 Python 类属性一起使用的文档字符串?