我写了一个自定义的lambda函数,需要在dataframe中的groupby操作之后应用。lambda函数将所有唯一字符串连接在一起,并使用适当的joiner,如“,”。我试图在Python的Dask库中实现相同的功能。然而,我得到了下面的错误信息。有人能指导我如何在Dask中实现这个lambda函数吗?
Pandas实现:
A = pd.DataFrame(data = {"A": ["saad", "saad", "saad", "saad", "nimra", "asad", "nimra", "nimra", "asad"],
"B": ["hello", "hello", "saad", "whatsup?", "yup", "nup", "saad", "saad", "nup"],
"C": ["hello", "hello", "saad", "whatsup?", "yup", "nup", "saad", "saad", "nup"]
}
)
A.groupby("A")["B"].unique().apply(', '. join)
A.groupby("A").agg(lambda s: ', '.join(s.unique()))
This code works perfectly fine and produces the correct output:
B C
A
asad nup nup
nimra yup, saad yup, saad
saad hello, saad, whatsup? hello, saad, whatsup?
Dask实现
我尝试在Dask中使用以下代码实现它:
x一个一个一个一个x一个一个二个x
1条答案
按热度按时间kwvwclae1#
您可以改用dask.dataframe.apply函数。