我有一个大型数据帧(几百万行).
我希望能够对它进行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)
Run Code Online (Sandbox Code Playgroud)
但这似乎很啰嗦,并不能保证大小相等.特别是如果索引是稀疏的或非整数的或其他什么.
有什么更好的方法吗?
谢谢!
如何遍历 Pandas DataFrame 的成对行?
例如:
content = [(1,2,[1,3]),(3,4,[2,4]),(5,6,[6,9]),(7,8,[9,10])]
df = pd.DataFrame( content, columns=["a","b","interval"])
print df
Run Code Online (Sandbox Code Playgroud)
输出:
a b interval
0 1 2 [1, 3]
1 3 4 [2, 4]
2 5 6 [6, 9]
3 7 8 [9, 10]
Run Code Online (Sandbox Code Playgroud)
现在我想做类似的事情
for (indx1,row1), (indx2,row2) in df.?
print "row1:\n", row1
print "row2:\n", row2
print "\n"
Run Code Online (Sandbox Code Playgroud)
哪个应该输出
row1:
a 1
b 2
interval [1,3]
Name: 0, dtype: int64
row2:
a 3
b 4
interval [2,4]
Name: 1, dtype: int64
row1:
a 3
b …Run Code Online (Sandbox Code Playgroud)