簡體   English   中英

python:pandas:如何基於groupby另一列在列中查找最大值

[英]python: pandas: how to find max value in a column based on groupby another column

我想根據一列 SERVER 對我的數據框進行分組,然后在另一列 JOB_ID 中找到最大值。 DF:

     SERVER   JOB_ID     LOG_FILE                 TIME
0    abc_123      1   1/abc_123/dep2/1/123.log  2019-12-05T05:06:16.346Z
1    abc_123     10  1/abc_123/dep2/10/123.log  2019-12-04T17:05:28.335Z
2    abc_123     11  1/abc_123/dep2/11/123.log  2019-12-04T20:27:03.988Z
3    abc_123     12  1/abc_123/dep2/12/123.log  2019-12-04T20:35:49.039Z
4    abc_123     13  1/abc_123/dep2/13/123.log  2019-12-04T20:42:36.890Z
5    abc_123     14  1/abc_123/dep2/14/123.log  2019-12-04T20:52:01.295Z
6    abc_123     15  1/abc_123/dep2/15/123.log  2019-12-04T20:58:07.132Z
7    abc_123     16  1/abc_123/dep2/16/123.log  2019-12-04T20:59:51.877Z
8    abc_123     17  1/abc_123/dep2/17/123.log  2019-12-04T21:00:23.458Z
9    abc_123     18  1/abc_123/dep2/18/123.log  2019-12-04T21:05:48.047Z
10   abc_123     19  1/abc_123/dep2/19/123.log  2019-12-05T03:10:39.325Z
11   abc_123      2   1/abc_123/dep2/2/123.log  2019-12-04T15:37:41.540Z
12   abc_123     20  1/abc_123/dep2/20/123.log  2019-12-05T04:09:39.221Z
13   abc_123     21  1/abc_123/dep2/21/123.log  2019-12-05T04:14:54.228Z
14   abc_123      3   1/abc_123/dep2/3/123.log  2019-12-04T15:41:38.340Z
15   abc_123      4   1/abc_123/dep2/4/123.log  2019-12-04T15:43:34.277Z
16   abc_123      5   1/abc_123/dep2/5/123.log  2019-12-04T15:56:18.647Z
17   abc_123      6   1/abc_123/dep2/6/123.log  2019-12-04T16:14:23.323Z
18   abc_123      7   1/abc_123/dep2/7/123.log  2019-12-04T16:19:22.126Z
19   abc_123      8   1/abc_123/dep2/8/123.log  2019-12-04T16:32:30.121Z
20   abc_123      9   1/abc_123/dep2/9/123.log  2019-12-04T16:53:54.236Z
21   abc_123      1   1/abc_123/dep_1/1/123.log  2019-11-30T06:20:16.528Z
22   abc_123     10  1/abc_123/dep_1/10/123.log  2019-12-03T07:10:38.320Z
23   abc_123     11  1/abc_123/dep_1/11/123.log  2019-12-03T09:19:33.350Z
24   abc_123     12  1/abc_123/dep_1/12/123.log  2019-12-03T09:51:49.835Z
25   abc_123     13  1/abc_123/dep_1/13/123.log  2019-12-03T10:43:19.727Z
26   abc_123     14  1/abc_123/dep_1/14/123.log  2019-12-04T06:11:52.125Z
27   abc_123     15  1/abc_123/dep_1/15/123.log  2019-12-04T06:33:58.416Z
28   abc_123     16  1/abc_123/dep_1/16/123.log  2019-12-04T06:48:18.057Z
29   abc_123      2   1/abc_123/dep_1/2/123.log  2019-11-30T16:45:13.983Z
30   abc_123      3   1/abc_123/dep_1/3/123.log  2019-11-30T18:19:14.364Z
31   abc_123      4   1/abc_123/dep_1/4/123.log  2019-12-02T08:38:01.766Z
32   abc_123      5   1/abc_123/dep_1/5/123.log  2019-12-02T10:12:45.500Z
33   abc_123      6   1/abc_123/dep_1/6/123.log  2019-12-02T12:04:03.326Z
34   abc_123      7   1/abc_123/dep_1/7/123.log  2019-12-02T15:13:11.312Z
35   abc_123      8   1/abc_123/dep_1/8/123.log  2019-12-03T05:44:47.436Z
36   abc_123      9   1/abc_123/dep_1/9/123.log  2019-12-03T06:16:05.041Z

當我在代碼下面運行時

DF_FINAL = DF.groupby(['SERVER']).agg({'JOB_ID':'max'})

低於輸出

          SERVER   JOB_ID     LOG_FILE                 TIME
20   abc_123      9   1/abc_123/dep2/9/123.log  2019-12-04T16:53:54.236Z

預期產出

13   abc_123     21  1/abc_123/dep2/21/123.log  2019-12-05T04:14:54.228Z

我參考了這個鏈接 但它沒有給我正確的答案。

JOB_ID不是數字,而是字符串(dtype 是object ),因此需要在解決方案之前將其轉換為數字:

DF.JOB_ID = DF.JOB_ID.astype(int)

如果上面的解決方案不起作用,因為一些非數值使用:

DF.JOB_ID = pd.to_numeric(DF.JOB_ID, errors='coerce')

最后使用DataFrameGroupBy.idxmax用於索引標識DataFrame.loc

DF_FINAL = DF.loc[DF.groupby('SERVER')['JOB_ID'].idxmax()]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM