【问题标题】:Groupby multiple columns and aggregation with daskGroupby 多列和聚合与 dask
【发布时间】:2019-11-29 13:05:51
【问题描述】:

dask 数据框如下所示:

A     B     C     D
1     foo   xx    this
1     foo   xx    belongs
1     foo   xx    together
4     bar   xx    blubb

我想按列 A、B、C 分组,并在 D 之间用空格连接字符串以获取

A     B     C     D
1     foo   xx    this belongs together
4     bar   xx    blubb

我知道如何用 pandas 做到这一点:

df_grouped = df.groupby(['A','B','C'])['D'].agg(' '.join).reset_index()

如何使用 dask 实现这一点?

【问题讨论】:

    标签: python pandas dataframe pandas-groupby dask


    【解决方案1】:
    ddf = ddf.groupby(['A','B','C'])['D'].apply(lambda row: ' '.join(row)).reset_index()
    ddf.compute()
    

    输出:

    Out[75]: 
       A    B   C                      D
    0  1  foo  xx  this belongs together
    0  4  bar  xx                  blubb
    

    【讨论】:

      【解决方案2】:

      您可以使用 CustomAggregation,其中每个块和聚合操作都是您的 ' '.join 方法。

      https://docs.dask.org/en/latest/dataframe-api.html#custom-aggregation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-01
        • 2018-04-07
        • 1970-01-01
        • 2020-11-05
        • 2019-10-12
        • 2023-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多