Dask groupby和apply:值错误预期的轴有6个元素,新值有5个元素

时间:2018-05-16 12:20:01

标签: dask

我正在尝试基于密钥折叠数据帧的行。我的文件很大,pandas会抛出内存错误。我目前正在尝试使用dask。我在这里附上代码片段。

def f(x):
        p = x.groupby(id).agg(''.join).reset_index()
        return p
metadf = pd.DataFrame(columns=['c1','p1','pd1','d1'])
df = df.groupby(idname).apply(f, meta=metadf).reset_index().compute()

pmetadf的结构相同。两个数据帧的形状都是相同的。 当我执行此操作时,我收到以下错误:

" ValueError:长度不匹配:预期的轴有6个元素,新值有5个元素"

我在这里缺少什么?有没有其他方法可以根据dask中的键来折叠行?

The task in hand, to do the following sample in a dask dataframe
Input csv file :
key,c1,c2,c3......,cn
1,car,phone,cat,.....,kite
2,abc,def,hij,.......,pot
1,yes,no,is,.........,hello
2,hello,yes,no,......,help

Output csv file:
key,c1,c2,c3,.......,cn
1,caryes,phoneno,catis,.....,kitehello
2,abchello,defyes,hijno,....,pothelp

1 个答案:

答案 0 :(得分:1)

在这种情况下,meta=对应df.groupby(...).apply(f)的输出,而不仅仅是f的输出。也许这些有些微妙的不同?

我会首先不提供meta=来解决这个问题。 Dask.dataframe会给你一个警告,要求你明确,但如果能够通过你的函数运行一些样本数据来确定正确的dtypes和列,那么事情应该有希望进展。