How to use Dask Pivot_table?

时间:2018-03-25 20:16:35

标签: dataframe pivot-table dask

I'm Trying to use Pivot_table on Dask with the following dataframe:

    date    store_nbr   item_nbr    unit_sales  year    month
0   2013-01-01  25       103665      7.0        2013      1
1   2013-01-01  25       105574      1.0        2013      1
2   2013-01-01  25       105575      2.0        2013      1
3   2013-01-01  25       108079      1.0        2013      1
4   2013-01-01  25       108701      1.0        2013      1

When I try to pivot_table as follows:

ddf.pivot_table(values='unit_sales', index={'store_nbr','item_nbr'}, 
                                  columns={'year','month'}, aggfunc={'mean','sum'})

I got this error:

ValueError: 'index' must be the name of an existing column

And If I just use only one value on index and columns parameters as follows:

df.pivot_table(values='unit_sales', index='store_nbr', 
                                  columns='year', aggfunc={'sum'})

I got this error:

ValueError: 'columns' must be category dtype

1 个答案:

答案 0 :(得分:1)

该错误告诉您dask数据框期望columns关键字中使用的列是分类dtype。它需要这样才能正确定义列,即使在延迟操作期间也是如此。您可以按如下方式完成此操作:

df = df.categorize(columns=['year'])