【问题标题】:Will dask map_partitions(pd.cut, bins) actually operate on entire dataframe?dask map_partitions(pd.cut, bins) 实际上会在整个数据帧上运行吗?
【发布时间】:2021-06-03 02:45:36
【问题描述】:

我需要在 dask 数据帧上使用 pd.cut。

This answer 表示 map_partitions 将通过传递 pd.cut 作为函数来工作。

似乎 map_partitions 一次只将一个分区传递给函数。但是, pd.cut 将需要访问我的 df 的整个列才能创建垃圾箱。所以,我的问题是:在这种情况下,map_partitions 是否会真正在整个数据帧上运行,还是我会用这种方法得到不正确的结果?

【问题讨论】:

    标签: python dask


    【解决方案1】:

    在您的问题中,您正确地确定了为什么应明确提供垃圾箱。

    通过指定确切的 bin 切割(基于某些计算或外部推理),您可以确保 dask 所做的事情在分区之间具有可比性。

    # this does not guarantee comparable cuts
    ddf['a'].map_partitions(pd.cut)
    
    # this ensures the cuts are as per the specified bins
    ddf['a'].map_partitions(pd.cut, bins)
    

    如果您想以自动方式生成 bin,一种方法是获取感兴趣列的最小值/最大值并使用 np.linspace 生成 bin:

    # note that computation is needed to give
    # actual (not delayed) values to np.linspace
    bmin, bmax = dask.compute(ddf['a'].min(), ddf['a'].max)
    
    # specify the number of desired cuts here
    bins = np.linspace(bmin, bmax, num=123)
    

    【讨论】:

    • 谢谢!如果我只为箱指定一个整数,那么也不能保证可比较的削减也是正确的吗? dask 有没有办法让它在整个列上运行某些功能(pd.cut 或其他)?由于 .apply 仅允许轴为 1,因此 .apply 不是一个选项。理想情况下,我会为 bin 传递一个整数,然后让 pd.cut 确定 bin,而不是先分别确定它们。
    • 查看更新的答案,但由于中间计算,它并不理想。
    • 非常感谢。这完成了我想做的事情。
    猜你喜欢
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2022-08-06
    相关资源
    最近更新 更多