我想找到替代方案pandas.dataframe.sort_value在 dask 中运行。
我走过来了设置索引,但它会按单个列排序。
如何对 Dask 数据框的多列进行排序?
目前为止Dask似乎还不支持多列排序。但是,创建一个新列来连接已排序列的值可能是一种可用的解决方法。
d['new_column'] = d.apply(lambda r: str([r.col1,r.col2]), axis=1)
d = d.set_index('new_column')
d = d.map_partitions(lambda x: x.sort_index())
编辑:
如果你想按两个字符串排序,上面的方法就有效。我建议创建整数(或字节)列,然后使用struct.pack
创建一个新的复合字节列。例如,如果col1_dt
是一个日期时间并且col2
是一个整数:
import struct
# create a timedelta with seconds resolution.
# i know this is the resolution is correct
d['col1_int'] = ((d['col1_dt'] -
d['col1_dt'].min())/np.timedelta64(1,'s')
).astype(int)
d['new_column'] = d.apply(lambda r: struct.pack("ll",r.col1_int,r.col2))
d = d.set_index('new_column')
d = d.map_partitions(lambda x: x.sort_index())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)