【问题标题】:Multiple indexing with multiple idxmin() and idmax() in one aggregate in pandas在 pandas 的一个聚合中使用多个 idxmin() 和 idmax() 进行多重索引
【发布时间】:2020-09-29 09:34:15
【问题描述】:

在 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 的一个 agg 函数中合并三种不同的索引。这是 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

【问题讨论】:

  • 你可以在 pandas 中分部分做,同时很难,也许一些字典应用代码可以做到

标签: python r python-3.x pandas data.table


【解决方案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

【讨论】:

    【解决方案2】:

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

    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
    

    加速约 3 倍。

    【讨论】:

      【解决方案3】:

      python 中的tidyverse 方式怎么样:

      >>> 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 包的作者。如果您有任何问题,请随时提交问题。

      【讨论】:

        猜你喜欢
        • 2018-12-01
        • 2012-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-19
        • 2019-01-10
        相关资源
        最近更新 更多