我从如下数据帧开始:
id tof
0 43.0 1999991.0
1 43.0 2095230.0
2 43.0 4123105.0
3 43.0 5560423.0
4 46.0 2098996.0
5 46.0 2114971.0
6 46.0 4130033.0
7 46.0 4355096.0
8 82.0 2055207.0
9 82.0 2093996.0
10 82.0 4193587.0
11 90.0 2059360.0
12 90.0 2083762.0
13 90.0 2648235.0
14 90.0 4212177.0
15 103.0 1993306.0
.
.
.
最终,我的目标是创建一个非常长的二维数组,其中包含具有相同id的所有项的组合(对于id为43的行):
[(1993306.0, 2105441.0), (1993306.0, 3972679.0), (1993306.0, 3992558.0), (1993306.0, 4009044.0), (2105441.0, 3972679.0), (2105441.0, 3992558.0), (2105441.0, 4009044.0), (3972679.0, 3992558.0), (3972679.0, 4009044.0), (3992558.0, 4009044.0),...]
除了把所有元组都改成数组,这样我就可以在遍历所有的id号之后转置数组。你知道吗
很自然地,我想到了itertools,我的第一个想法是用df.groupby('id')
做一些事情,这样它就可以在内部将itertools应用于每个具有相同id的组,但我想,对于我拥有的百万行数据文件来说,这绝对需要永远的时间。你知道吗
有没有一个矢量化的方法可以做到这一点?你知道吗
IIUC公司:
说明
这是一个列表理解,返回由数据帧构造函数包装的列表列表。Look up comprehensions to understand better.
Groupby确实有效:
输出:
如果希望元素不重复,可以使用
相关问题 更多 >
编程相关推荐