在大熊猫的一个聚合中使用多个 idxmin() 和 idmax() 进行多重索引

在 R data.table 中,可以很容易地在一个聚合中使用 argmin 或 argmax 函数在多个列上进行聚合。例如对于 DT:


> DT = data.table(id=c(1,1,1,2,2,2,2,3,3,3), col1=c(1,3,5,2,5,3,6,3,67,7), col2=c(4,6,8,3,65,3,5,4,4,7), col3=c(34,64,53,5,6,2,4,6,4,67))

> DT

    id col1 col2 col3

 1:  1    1    4   34

 2:  1    3    6   64

 3:  1    5    8   53

 4:  2    2    3    5

 5:  2    5   65    6

 6:  2    3    3    2

 7:  2    6    5    4

 8:  3    3    4    6

 9:  3   67    4    4

10:  3    7    7   67


> DT_agg = DT[, .(agg1 = col1[which.max(col2)]

                , agg2 = col2[which.min(col3)]

                , agg3 = col1[which.max(col3)])

              , by= id]

> DT_agg

   id agg1 agg2 agg3

1:  1    5    4    3

2:  2    5    3    5

3:  3    7    4    7

agg1 是 col1 的值,其中 col2 的值最大,按 id 分组。


agg2 是 col2 的值,其中 col3 的值最小,按 id 分组。


agg3 是 col1 的值,其中 col3 的值最大,按 id 分组。


在 Pandas 中,这怎么可能,使用 groupby 和 agg 在一个聚合操作中完成所有三个聚合?我不知道如何在 Python 的一个聚合函数中合并三个不同的索引。这是 Python 中的数据框:


DF =pd.DataFrame({'id':[1,1,1,2,2,2,2,3,3,3], 'col1':[1,3,5,2,5,3,6,3,67,7], 'col2':[4,6,8,3,65,3,5,4,4,7], 'col3':[34,64,53,5,6,2,4,6,4,67]})


DF

Out[70]: 

   id  col1  col2  col3

0   1     1     4    34

1   1     3     6    64

2   1     5     8    53

3   2     2     3     5

4   2     5    65     6

5   2     3     3     2

6   2     6     5     4

7   3     3     4     6

8   3    67     4     4

9   3     7     7    67


呼啦一阵风
浏览 260回答 3
3回答

慕神8447489

你可以试试这个,DF.groupby('id').agg(agg1=('col1',lambda x:x[DF.loc[x.index,'col2'].idxmax()]),                     agg2 = ('col2',lambda x:x[DF.loc[x.index,'col3'].idxmin()]),                     agg3 = ('col1',lambda x:x[DF.loc[x.index,'col3'].idxmax()]))    agg1  agg2  agg3id1      5     4     32      5     3     53      7     4     7

森栏

玩弄这个问题,主要是为了看看我是否可以提高原始解决方案的速度。这比命名聚合更快。grp = df.groupby("id")        pd.DataFrame({ "col1": df.col1[grp.col2.idxmax()].array,                       "col2": df.col2[grp.col3.idxmin()].array,                       "col3": df.col1[grp.col3.idxmax()].array},                       index=grp.indices)    col1    col2    col31   5       4       32   5       3       53   7       4       7加速~3x。

ABOUTYOU

tidyversepython中的一种方式怎么样:>>> from datar.all import f, tibble, group_by, which_max, which_min, summarise>>>&nbsp;>>> DF = tibble(...&nbsp; &nbsp; &nbsp;id=[1,1,1,2,2,2,2,3,3,3],&nbsp;...&nbsp; &nbsp; &nbsp;col1=[1,3,5,2,5,3,6,3,67,7],...&nbsp; &nbsp; &nbsp;col2=[4,6,8,3,65,3,5,4,4,7],&nbsp;...&nbsp; &nbsp; &nbsp;col3=[34,64,53,5,6,2,4,6,4,67]... )>>>&nbsp;>>> DF >> group_by(f.id) >> summarise(...&nbsp; &nbsp; &nbsp;agg1=f.col1[which_max(f.col2)],...&nbsp; &nbsp; &nbsp;agg2=f.col2[which_min(f.col3)],...&nbsp; &nbsp; &nbsp;agg3=f.col1[which_max(f.col3)]... )&nbsp; &nbsp; &nbsp; &nbsp;id&nbsp; &nbsp; agg1&nbsp; &nbsp; agg2&nbsp; &nbsp; agg3&nbsp; <int64> <int64> <int64> <int64>0&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;31&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; &nbsp;52&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; &nbsp; &nbsp; &nbsp;7&nbsp; &nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;7我是datar包的作者。如果您有任何问题,请随时提交问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python