问题描述
我有巨大的 (df
) 按 Date_start
和 ID
排序:
Date_start ID Start_flag End_flag Date_end
01-01-2019 100 1 0 01-02-2019
01-02-2019 100 0 0 01-03-2019
01-03-2019 100 0 0 01-04-2019
01-06-2019 100 0 0 01-07-2019
01-09-2019 500 1 0 01-10-2019
01-11-2019 500 0 0 01-12-2019
01-05-2020 500 0 0 01-06-2020
01-06-2020 500 0 0 01-07-2020
01-07-2020 500 0 0 01-08-2020
01-08-2020 500 0 0 01-09-2020
01-09-2020 700 1 0 01-12-2020
01-01-2021 700 0 0 01-04-2021
01-04-2021 700 0 1 01-07-2021
我还有一个函数,它有助于定义每一行 date_ranges 中的重叠。
我需要为由 df
分组的 ID
应用此函数,如下所示:
df.groupby('ID').apply(detect_overlapping)
查看最后,我按唯一 ID 将 df
拆分为块,最后 df_chunked
- 是每个块中有 25000 个 ID 的数据帧列表。
为了在每个块中并行计算,我使用 multiprocessing.Pool
:
def applyParallel(grouped_df,func):
num_cores = 8
with Pool(num_cores) as pool:
result_list = pool.map(func,[group for name,group in grouped_df])
pool.close()
pool.join()
return pd.concat(result_list,axis=0)
我将它应用到 df_chunked
的循环中:
result = []
for i in range(0,len(df_chunked)):
print('chunk# ',i)
gr_data = df_chunked[i].groupby('ID')
df_with_target = applyParallel(gr_data,detect_overlapping)
result.append(df_with_target)
上述方法的性能不佳。我还尝试测试 pandarallel
,但它因巨大的 df
而崩溃。
为了减少它,我试图找到如何并行化所有 df_chunked
和每组 [df_chunked[0],df_chunked[1]],[df_chunked[2],df_chunked[3]]...,
运行 applyParallel(gr_data,detect_overlapping)
内部的解决方案。
问题:有没有什么办法可以用我描述的更好的方法来解决这个任务? 谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)