【问题标题】:Can dask be used to groupby and recode out of core?dask 可以用来分组和重新编码核心吗?
【发布时间】:2017-01-18 11:40:14
【问题描述】:

我有 8GB 的​​ csv 文件和 8GB 的​​ RAM。每个文件每行有两个字符串,格式如下:

a,c
c,a
f,g
a,c
c,a
b,f
c,a

对于较小的文件,我删除重复项,计算前两列中每行的副本数,然后将字符串重新编码为整数as follows:

import pandas as pd
from sklearn.preprocessing import LabelEncoder
df = pd.read_csv("file.txt", header=None, prefix="ID_")

# Perform the groupby (before converting letters to digits).
df = df.groupby(['ID_0', 'ID_1']).size().rename('count').reset_index()

# Initialize the LabelEncoder.
le = LabelEncoder()
le.fit(df[['ID_0', 'ID_1']].values.flat)

# Convert to digits.
df[['ID_0', 'ID_1']] = df[['ID_0', 'ID_1']].apply(le.transform)

这给出了:

   ID_0  ID_1  count
0     0     1      2
1     1     0      3
2     2     4      1
3     4     3      1

这正是这个玩具示例所需要的。

对于较大的文件,由于 RAM 不足,我无法执行这些步骤。

我可以想象,可以将 unix 排序和定制的 python 解决方案结合起来,对数据进行多次传递以处理我的数据集。但有人建议 dask 可能是合适的。阅读文档后我仍然不清楚。

可以使用 dask 进行这种核心外处理,还是有其他一些核心外的 pandas 解决方案?

【问题讨论】:

  • dask 是一个明智的选择:请参阅下面的答案,遇到问题不要放弃。

标签: python pandas dask


【解决方案1】:

假设分组数据框符合您的记忆,您必须对代码进行的更改应该非常小。这是我的尝试:

import pandas as pd
from dask import dataframe as dd
from sklearn.preprocessing import LabelEncoder

# import the data as dask dataframe, 100mb per partition
# note, that at this point no data is read yet, dask will read the files
# once compute or get is called.
df = dd.read_csv("file.txt", header=None, prefix="ID_", blocksize=100000000)

# Perform the groupby (before converting letters to digits).
# For better understanding, let's split this into two parts:
#     (i) define the groupby operation on the dask dataframe and call compute()
#     (ii) compute returns a pandas dataframe, which we can then use for further analysis
pandas_df = df.groupby(['ID_0', 'ID_1']).apply(lambda x: len(x), columns=0).compute()
pandas_df = pandas_df.rename('count').reset_index()

# Initialize the LabelEncoder.
le = LabelEncoder()
le.fit(pandas_df[['ID_0', 'ID_1']].values.flat)

# Convert to digits.
pandas_df[['ID_0', 'ID_1']] = pandas_df[['ID_0', 'ID_1']].apply(le.transform)

pandas 中一种可能的解决方案是读取块中的文件(将 chunksize 参数传递给 read_csv),在单个块上运行 groupby 并组合结果。


以下是在纯 python 中解决问题的方法:

counts = {}
with open('data') as fp:
    for line in fp:
        id1, id2 = line.rstrip().split(',')
        counts[(id1, id2)] = 1 + counts.get((id1, id2), 0)

df = pd.DataFrame(data=[(k[0], k[1], v) for k, v in counts.items()],
                  columns=['ID_0', 'ID_1', 'count'])
# apply label encoding etc.
le = LabelEncoder()
le.fit(df[['ID_0', 'ID_1']].values.flat)

# Convert to digits.
df[['ID_0', 'ID_1']] = df[['ID_0', 'ID_1']].apply(le.transform)

【讨论】:

  • 谢谢。您的熊猫建议似乎很困难,因为我不确定您将如何结合结果。你能添加一些代码吗?
  • pandas 解决方案将涉及组合单个 groupby 结果(通过为每个块添加 (ID_0, ID_1)-tuples 的计数)。解决您的特定问题的最简单方法是简单地读取文件,边走边算。我已经为此添加了代码 - 或者您更喜欢执行大型 groupbys 的一般配方?
  • 感谢您的编辑。使用 dict 的一个问题是它在 Python 中的内存效率非常低。
  • LabelEncoder 在大型数据集上非常慢。这是一个已知的缺陷,但同时 dict 更快。
猜你喜欢
  • 2015-04-05
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-17
  • 2019-10-11
相关资源
最近更新 更多