【问题标题】:dask create combined column to simulate sort by 2 columnsdask 创建组合列以模拟按 2 列排序
【发布时间】:2021-10-26 13:51:19
【问题描述】:

目前df.sort_values in dask只接受1列排序。

我有一个具有这种结构的大文件

input data

我不知道如何先按整数列排序数据,然后按日期排序

  • 2000-01-01 ; 43000
  • 2000-01-02 ; 43000
  • 2000-01-01 ; 25000
  • 2000-01-02 ; 25000

我认为创建一个组合列并对其进行排序将是最佳选择。问题是我不知道如何创建一个完成此操作的列。也许还有另一种选择可以做到这一点,而无需在 Dask 中创建组合列...

谢谢!

【问题讨论】:

标签: python dask dask-dataframe


【解决方案1】:

假设d['col1']datetime-type,而d['col2']int-type:

import struct
import numpy as np

# create a timedelta with days resolution as int
d['col1_int'] = ((d['col1_dt'] -
                  d['col1_dt'].min())/np.timedelta64(1,'D')
                ).astype(int)

d['sort_col'] = d.apply(lambda r: struct.pack("ll",r.col1_int,r.col2))
d = d.set_index('sort_col')
d = d.map_partitions(lambda x: x.sort_index())

this 回答中重新设计

【讨论】:

  • Dask 不允许为 sort_values 添加多个列......
  • @lsgrep 已更新
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多