【发布时间】:2014-10-31 04:59:07
【问题描述】:
我有一个大数据框(数百万行)。
我希望能够对其执行 groupby 操作,但只是按任意连续(最好是大小相等)的行子集进行分组,而不是使用各个行的任何特定属性来决定它们进入哪个组。
用例:我想通过 IPython 中的并行映射将函数应用于每一行。哪些行进入哪个后端引擎并不重要,因为该函数一次根据一行计算结果。 (至少在概念上;实际上它是矢量化的。)
我想出了这样的东西:
# Generate a number from 0-9 for each row, indicating which tenth of the DF it belongs to
max_idx = dataframe.index.max()
tenths = ((10 * dataframe.index) / (1 + max_idx)).astype(np.uint32)
# Use this value to perform a groupby, yielding 10 consecutive chunks
groups = [g[1] for g in dataframe.groupby(tenths)]
# Process chunks in parallel
results = dview.map_sync(my_function, groups)
但这似乎很冗长,并且不能保证大小相等的块。特别是如果索引是稀疏或非整数或其他。
对更好的方法有什么建议吗?
谢谢!
【问题讨论】:
标签: python pandas parallel-processing ipython