使用Dask对多列进行分组并聚合

4

Dask Dataframe 的外观如下:

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中实现呢?

2个回答

3
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

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接