为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼啦一阵风 2023-01-04 16:13:47
在 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    410:  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 agg31:  1    5    4    32:  2    5    3    53:  3    7    4    7agg1 是 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]})DFOut[70]:    id  col1  col2  col30   1     1     4    341   1     3     6    642   1     5     8    533   2     2     3     54   2     5    65     65   2     3     3     26   2     6     5     47   3     3     4     68   3    67     4     49   3     7     7    67
查看完整描述

3 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

你可以试试这个,


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  agg3

id

1      5     4     3

2      5     3     5

3      7     4     7



查看完整回答
反对 回复 2023-01-04
?
森栏

TA贡献1810条经验 获得超5个赞

玩弄这个问题,主要是为了看看我是否可以提高原始解决方案的速度。这比命名聚合更快。


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    col3

1   5       4       3

2   5       3       5

3   7       4       7

加速~3x。


查看完整回答
反对 回复 2023-01-04
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

tidyversepython中的一种方式怎么样:


>>> from datar.all import f, tibble, group_by, which_max, which_min, summarise

>>> 

>>> DF = tibble(

...     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 >> group_by(f.id) >> summarise(

...     agg1=f.col1[which_max(f.col2)],

...     agg2=f.col2[which_min(f.col3)],

...     agg3=f.col1[which_max(f.col3)]

... )

       id    agg1    agg2    agg3

  <int64> <int64> <int64> <int64>

0       1       5       4       3

1       2       5       3       5

2       3       7       4       7

我是datar包的作者。如果您有任何问题,请随时提交问题。



查看完整回答
反对 回复 2023-01-04
  • 3 回答
  • 0 关注
  • 259 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号