【问题标题】:Slicing operation with Dask in a optimal way with Python使用 Python 以最佳方式使用 Dask 进行切片操作
【发布时间】:2022-08-21 08:11:51
【问题描述】:

我对切片操作有几个问题。 在 pandas 中,我们可以进行如下操作:

df[\"A\"].iloc[0]
df[\"B\"].iloc[-1]

# here df[\"A\"],df[\"B\"] is sorted

因为我们不能用 Dask 做这个(切片和 Multiple_col_sorting)(我不是 100% 确定),所以我用另一种方法来做

df[\"A\"]=df.sort_values(by=[\'A\'])
first=list(df[\"A\"])[0]
df[\"B\"]=df.sort_values(by=[\'B\'])
end=list(df[\"B\"])[-1]

当数据帧很大时,这种方式真的很耗时,有没有其他方法可以做这个操作?

https://docs.dask.org/en/latest/dataframe-indexing.html

https://docs.dask.org/en/latest/array-slicing.html

我尝试使用它,但它不起作用。

    标签: python pandas dataframe dask


    【解决方案1】:

    索引或 Dask 与 Pandas 不同,因为 Pandas 是数据的全局排序。对于每个分区,Dask 的索引从 1 到 N,因此有多个索引值为 1 的项目。这就是我认为不允许连续使用 iloc 的原因。

    为此,请使用

    第一:https://docs.dask.org/en/latest/generated/dask.dataframe.DataFrame.first.html

    最后的: https://docs.dask.org/en/latest/generated/dask.dataframe.DataFrame.last.html

    对于分布在多台机器上的大型数据帧,排序是一项非常昂贵的操作,而 first 和 last 是非常可并行化的操作,因为它可以在每个分区中完成,然后在每个分区的结果中再次执行。

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 2022-01-12
      • 2019-08-07
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 2011-04-24
      • 2023-04-08
      • 1970-01-01
      相关资源
      最近更新 更多