【问题标题】:How to convert Dask Object columns into decimal columns如何将 Dask Object 列转换为十进制列
【发布时间】:2022-01-24 14:46:42
【问题描述】:

我有这个功能

def dec(x):
    """Convert to Decimal and remove exponent and trailing zeros"""
    if not x:
        return Decimal(0)
    if not isinstance(x, Decimal):
        x = Decimal(str(x))
    return x.quantize(Decimal(1)) if x == x.to_integral() else x.normalize()

在熊猫中我会这样做

df['price'].apply(dec)

但是,dask 不支持这一点,那么将列转换为十进制类型的另一种方法是什么?

【问题讨论】:

  • 这里已经回答了这个问题:参考this

标签: python python-3.x decimal dask dask-dataframe


【解决方案1】:

Dask DataFrame 确实支持应用,它适用于您的示例:

ds = dd.from_pandas(s, npartitions=2)
ds.apply(dec).compute()

不过,总的来说,我建议像 Sultan 所展示的那样使用 map_paritions

您还可以查看此博客:Parallelize pandas apply() and map() with Dask DataFrame,其中详细讨论了这些功能。

【讨论】:

    【解决方案2】:

    假设您的 dask 数据帧被称为 ddf.map_partitions 应该解决它:

    def pandas_wrap(df):
       df['new_price'] = df['price'].apply(dec)
       # potentially some other pandas code
       return df
    
    ddf = ddf.map_partitions(pandas_wrap)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-13
      • 2013-10-08
      • 2023-02-02
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      相关资源
      最近更新 更多