简体   繁体   English

按列查找连续五行的最大值和最小值

[英]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 项分组的索引,然后使用groupbyagg

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.

相关问题 根据另一个列值在列中保留具有特定值的连续行中的最大行 - Keep maximum row in consecutive rows with specific value in column based on another column value 正则表达式找到五个连续的辅音 - Regex to find five consecutive consonants 如何在Pandas Dataframe中找到n行数据的最小或最大列? - How can I find a minimum or maximum of a column for 'n' number of rows of data in Pandas Dataframe? 如何使用函数式编程来迭代和查找列表中五个连续数字的最大乘积? - How to use functional programming to iterate and find maximum product of five consecutive numbers in a list? 查找元组中最小元素的最大值 - Find maximum value of minimum elements in tuple python 找到子树的最小值和最大值 - python find sub tree minimum and maximum value 我需要找到最大值和最小值 Rowwise - I need to find Maximum and Minimum value Rowwise 查找文本文件的最小值,最大值和平均值 - Find minimum, maximum, and average value of a text file 如何在一个具有相同值(字符串)的数据框中找到两个连续的行,并在它们之间添加更多行? - how to find two consecutive rows in a dataframe with same value(string) for a column and add more rows between them? 捕获连续行中列值的变化 - Capturing a change in value of a column in consecutive rows
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM