Dask rolling function by group syntax

这一生的挚爱 提交于 2019-12-30 11:31:54

问题


I struggled for a while with the syntax to work for calculating a rolling function by group for a dask dataframe. The documentation is excellent, but in this case does not have an example.

The working version I have is as follows, from a csv that contains a text field with User ids and and x, y, and z column:

ddf = read_csv('./*.csv')
ddf.groupby(ddf.User).x.apply(lambda x: x.rolling(5).mean(), meta=('x', 'f8')).compute()

Is this the recommended syntax for rolling functions applied by group within dask DataFrames, or is there a recommended alternative?


回答1:


In order to retain the groups in the result:

ddf.groupby(by=User).apply(lambda df_g: df_g['x'].rolling(5).mean(), meta=('x', 'f8')).compute()


来源:https://stackoverflow.com/questions/42172130/dask-rolling-function-by-group-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!