python - Groupby 多列和聚合与 dask

标签 python pandas dataframe pandas-groupby dask

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

最佳答案

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

关于python - Groupby 多列和聚合与 dask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59105423/

相关文章:

python - 尝试对 Pandas 使用替换方法

python - Pandas - 为 Pandas 中的每个组插入空白行

r - 操作数据框列中的字符串

python - 在python中按键/id划分行中的值

python - AWS CloudFormation - 如何上传一些 python/flask 代码

python - 在 numpy 中向量化索引操作

python - Pandas 过滤器,其中当前行字符串包含不同列中前一行的字符串

python3 os模块路径拼接错误?

python - 从文本文件创建 numpy 数组时出错

python - Sklearn Labelencoder 在编码新数据帧时保留编码值