基于groupby之后第二列中第一个最大值的一列的值

2024-04-19 04:33:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个如下所示的数据帧,其中我在ItrType上做了一个groupby:

  • 如何在Values列为最大值的每个组中的行的Start中获取值?你知道吗
  • 我只想要第一个max;有多行Values是max(20.402)。当我在groupby of Itr2&;Type1之后执行此操作时,我的预期答案应该是101(因为此时Values是最大值)。你知道吗
  • 我无法使用.loc,因为我已经使用了groupby。你知道吗
  • 我无法对此使用apply函数,因为它涉及两列。你知道吗

Itr Type Start Values 2 1 101 20.402 2 1 102 20.402 2 1 103 20.399 2 1 104 20.399 2 1 105 20.399 2 1 106 20.383 2 1 107 20.383 2 1 108 20.383 2 1 109 20.383 2 1 110 20.383 2 1 111 20.36 2 1 112 20.36 2 1 113 20.36 2 1 114 20.36 2 1 115 20.36 2 1 116 20.36 2 1 117 20.36 2 1 118 20.36 2 1 119 20.36 2 1 120 20.36 3 1 121 20.348 3 1 122 20.348 3 1 123 20.348 3 1 124 20.348 3 1 125 20.348 3 1 126 20.34 3 1 127 20.34 3 1 128 20.34 3 1 129 20.34 3 1 130 20.34 3 1 131 20.337 3 1 132 20.337 3 1 133 20.337 3 1 134 20.337 3 1 135 20.337 3 1 136 20.342 3 2 121 20.058 3 2 122 20.058 3 2 123 20.058 3 2 124 20.058 3 2 125 20.043 3 2 126 20.043 3 2 127 20.043 3 2 128 20.043 3 2 129 20.043 3 2 130 20.035 3 2 131 20.035 3 2 132 20.035 3 2 133 20.035 3 2 134 20.035 3 2 135 20.021

As suggested I have put a simpler df & tried to make my requirement a bit more clearer. Itr Type Time Val 2 3 101 3 2 3 102 4 2 3 103 5 2 3 104 6 2 3 105 6 2 3 106 5 2 3 107 1 1 2 101 11 1 2 102 12 1 2 103 13 1 2 104 18 1 2 105 15 1 2 106 10 4 5 101 21 4 5 102 22 4 5 103 27 4 5 104 29 4 5 105 25 4 5 106 26

我希望分组依据后的“Time”和“Max&Min”值(即在每个组中)位于不同的列中(一列表示Max,一列表示Min)


Tags: of数据答案timetypeminstartmax
1条回答
网友
1楼 · 发布于 2024-04-19 04:33:51

如果您需要原始DataFrame中的新列,我相信您需要^{}

g = df.groupby(['Itr','Type'])

df['max_val'] = g['Val'].transform('max')
df['min_val'] = g['Val'].transform('min')

df['time_by_first_max_val'] = (df.set_index('Time')
                                 .groupby(['Itr','Type'])['Val'].transform('idxmax').values)
print (df)
    Itr  Type  Time  Val  max_val  min_val  time_by_first_max_val
0     2     3   101    3        6        1                    104
1     2     3   102    4        6        1                    104
2     2     3   103    5        6        1                    104
3     2     3   104    6        6        1                    104
4     2     3   105    6        6        1                    104
5     2     3   106    5        6        1                    104
6     2     3   107    1        6        1                    104
7     1     2   101   11       18       10                    104
8     1     2   102   12       18       10                    104
9     1     2   103   13       18       10                    104
10    1     2   104   18       18       10                    104
11    1     2   105   15       18       10                    104
12    1     2   106   10       18       10                    104
13    4     5   101   21       29       21                    104
14    4     5   102   22       29       21                    104
15    4     5   103   27       29       21                    104
16    4     5   104   29       29       21                    104
17    4     5   105   25       29       21                    104
18    4     5   106   26       29       21                    104

^{}如果需要聚合值:

df2 = (df.set_index('Time')
         .groupby(['Itr','Type'], sort=False)['Val']
         .agg([('max_val', 'max'),('min_val', 'min'),('time_by_first_max_val', 'idxmax')])
         .reset_index())
print (df2)

   Itr  Type  max_val  min_val  time_by_first_max_val
0    2     3        6        1                    104
1    1     2       18       10                    104
2    4     5       29       21                    104

相关问题 更多 >