[英]Find maximum and minimum value of five consecutive rows by column
I want to get the maximum and minimum value of some columns grouped by 5 consecutive values.我想获得按 5 个连续值分组的某些列的最大值和最小值。 Example, I want to have maximum by a and minimum by b, of 5 consecutive rows
例如,我希望连续 5 行的最大值为 a,最小值为 b
a b
0 1 2
1 2 3
2 3 4
3 2 5
4 1 1
5 3 6
6 2 8
7 5 2
8 4 6
9 2 7
I want to have我希望有
a b
0 3 1
1 5 2
(Where 3 is the maximum of 1,2,3,2,1 and 1 is the minumum of 2,3,4,5,1, and so on) (其中 3 是 1,2,3,2,1 的最大值,1 是 2,3,4,5,1 的最小值,依此类推)
Use integer division ( //
) to form the index for grouping by every 5 items, and then use groupby
and agg
:使用 integer 除法 (
//
) 形成每 5 项分组的索引,然后使用groupby
和agg
:
out = df.groupby(df.index // 5).agg({'a':'max', 'b':'min'})
Output: Output:
>>> out
a b
0 3 1
1 5 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.