我用pandas编写脚本,但为了提高效率,我需要切换到dask,但我不确定如何在dask中实现unstack和reindex?
这是我的pandas脚本的样子:
df_new = df.groupby(['Cars', 'Date'])['Durations'].mean().unstack(fill_value=0).reindex(columns=list_days,index=list_cars,fill_value=0).\
round().reset_index().fillna(0).round()发布于 2020-06-18 22:03:15
通常,.groupby()聚合的结果会很小,并且适合内存。如https://docs.dask.org/en/latest/dataframe-best-practices.html#reduce-and-then-use-pandas所示,您可以使用Dask进行大型聚合,然后使用pandas进行小型内存后处理。
df_new = (
df.groupby(['Cars', 'Date'])['Durations'].mean()
.compute() # turn the Dask DataFrame into a pandas dataframe
.unstack(fill_value=0).reindex(columns=list_days,index=list_cars,fill_value=0).
.round().reset_index().fillna(0).round()
)https://stackoverflow.com/questions/62447662
复制相似问题