我正在尝试基于密钥折叠数据帧的行。我的文件很大,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()
p
与metadf
具有相同的结构。两个数据帧的形状都是相同的。当我执行此操作时,我收到以下错误:
“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
在这种情况下,meta=
对应于df.groupby(...).apply(f)
的输出,而不仅仅是f
的输出。也许这些在一些微妙的方面有所区别?
我会首先不提供meta=
来解决这个问题。 Dask.dataframe会给你一个警告,要求你明确,但如果能够通过你的函数运行一些样本数据来确定正确的dtypes和列,那么事情应该有希望进展。