Dask.groupby 将多个分区合并为一个

Dask.groupby turns multiple partitions into one

我有一个dask.dataframe

df2 = dd.read_csv(path, dtype=dtypes, sep=',', error_bad_lines=False)

dask本身分割成220个分区

print(df2.npartitions)
>>220

我想使用 groupby 两次并将两个数据帧保存到文件中

coccurrence_df = df2.groupby(['h1_h2', 'hashtag1','hashtag2','user_id']).count().reset_index()\
            .groupby(['h1_h2', 'hashtag1','hashtag2']).message_id.count().reset_index()\
            .rename(columns={"message_id":"coccurrence"})
strong_edges_df = coccurrence_df[coccurrence_df['coccurrence']>1].to_csv(path1, compute=False)
weak_edges_df = coccurrence_df[coccurrence_df['coccurrence']==1].to_csv(path2, compute=False)
dask.compute(strong_edges_df,weak_edges_df)

为什么 coccurrence_df 被拆分为 1 个分区,而创建它的数据帧被拆分为 220 个分区?

print(coccurrence_df.npartitions)
>>1

我相信因此我正在失去并行性,对吗? 提前谢谢你

Groupby 聚合进行并行计算,但会产生单个分区输出。如果您有很多组并希望有一个多分区输出,那么请考虑对 groupby 聚合使用 split_out= 参数。

如果一切正常,我不建议这样做。我建议只使用默认值,直到明显表现不佳为止。