例如。考虑下面给出的数据框:
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/