我有一个 Pandas 数据框,如下所示:
+-----+---+---+--+--+
| | A | B | | |
+-----+---+---+--+--+
| 288 | 1 | 4 | | |
+-----+---+---+--+--+
| 245 | 2 | 3 | | |
+-----+---+---+--+--+
| 543 | 3 | 6 | | |
+-----+---+---+--+--+
| 867 | 1 | 9 | | |
+-----+---+---+--+--+
| 345 | 2 | 7 | | |
+-----+---+---+--+--+
| 122 | 3 | 8 | | |
+-----+---+---+--+--+
| 233 | 1 | 1 | | |
+-----+---+---+--+--+
| 346 | 2 | 6 | | |
+-----+---+---+--+--+
| 765 | 3 | 3 | | |
+-----+---+---+--+--+
我想要做的是从'A'列中1到3的范围内获取'B'列的最大值和最小值。
例如:
loop on A in range 1 to 3:
get max and min values from column 'B'
max = 6
min = 3
loop on the next range of A from 1 to 3:
get max and min values from column 'B'
max = 9
min = 7
loop on the next range of A from 1 to 3:
get max and min values from column 'B'
max = 6
min = 1
并将最小值和最大值添加到列中,如下:
+-----+---+---+--+----+
| | A | B |min|max|
+-----+---+---+--+----+
| 288 | 1 | 4 | 3 | 6 |
+-----+---+---+--+----+
| 245 | 2 | 3 | | |
+-----+---+---+--+----+
| 543 | 3 | 6 | | |
+-----+---+---+--+----+
| 867 | 1 | 9 | 7 | 9 |
+-----+---+---+--+----+
| 345 | 2 | 7 | | |
+-----+---+---+--+----+
| 122 | 3 | 8 | | |
+-----+---+---+--+----+
| 233 | 1 | 1 | 1 | 6 |
+-----+---+---+--+----+
| 346 | 2 | 6 | | |
+-----+---+---+--+----+
| 765 | 3 | 3 | | |
+-----+---+---+--+----+
df['range'] = 'range' + (np.arange(len(df.index)) // 3).astype(str)
and theng = df.groupby('range')
I am only on phone so unchecked. ` - jezrael