Python 值与一组值的矢量化比较
我有一个Python 值与一组值的矢量化比较,python,pandas,numpy,Python,Pandas,Numpy,我有一个pd.Dataframe,其中有一列包含如下值 df.iloc[:10]['occ'] Out[18]: 0 4220 1 205 2 7630 3 8965 4 430 5 3930 6 4230 7 5620 8 4040 9 4130 然后,我有另一个数据帧,其中包含不同组的start和end值。我想根据组的occ值为第一个数据帧分配组 start end group 1
pd.Dataframe
,其中有一列包含如下值
df.iloc[:10]['occ']
Out[18]:
0 4220
1 205
2 7630
3 8965
4 430
5 3930
6 4230
7 5620
8 4040
9 4130
然后,我有另一个数据帧,其中包含不同组的start
和end
值。我想根据组的occ
值为第一个数据帧分配组
start end
group
10 10 950
11 1000 3540
12 3600 3655
13 3700 3955
14 4000 4160
testAgainst = np.repeat(dfGroups['start'].values[np.newaxis, :], repeats=10, axis=0)
array([[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000]])
由于这些组不相交,我们有一个简单的双射。我计划为每个occ
值取最后一行的组索引,该索引小于所述occ
值
start end
group
10 10 950
11 1000 3540
12 3600 3655
13 3700 3955
14 4000 4160
testAgainst = np.repeat(dfGroups['start'].values[np.newaxis, :], repeats=10, axis=0)
array([[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000],
[ 10, 1000, 3600, 3700, 4000, 4200, 4300, 4700, 5000, 6000, 6200,
7000, 7700, 9000]])
现在,由于维度是(10,)
和(10,14)
,应该会发生自动广播。我希望能做到
df.iloc[:10]['occ'] < testAgainst
对于前两行,因为4220
大于4200
(以及之后的所有数字),205
大于10
然而,我明白了
Traceback (most recent call last):
File "/home/foo/.conda/envs/myenv3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-28-1bce7761846c>", line 1, in <module>
df.iloc[:10]['occ'] < testAgainst
File "/home/foo/.conda/envs/myenv3/lib/python3.5/site-packages/pandas/core/ops.py", line 832, in wrapper
return self._constructor(na_op(self.values, np.asarray(other)),
File "/home/foo/.conda/envs/myenv3/lib/python3.5/site-packages/pandas/core/ops.py", line 792, in na_op
result = getattr(x, name)(y)
ValueError: operands could not be broadcast together with shapes (10,) (10,14)
回溯(最近一次呼叫最后一次):
文件“/home/foo/.conda/envs/myenv3/lib/python3.5/site packages/IPython/core/interactiveshell.py”,第2881行,运行代码
exec(代码对象、self.user\u全局、self.user\n)
文件“”,第1行,在
df.iloc[:10][occ']
df
中有2500万行)系列
对象形成了一个一维标记数组[shape=(10,)
],与二维数组[shape=(1,14)
]相比
让我们考虑一下:ser=df.iloc[:10][occ']
如果你做了:
>>> ser.iloc[0] < testAgainst
array([[False, False, False, False, False, False, True, True, True,
True, True, True, True, True]], dtype=bool)
结果输出:
0 [False, False, False, False, False, False, Tru...
1 [False, True, True, True, True, True, True, Tr...
2 [False, False, False, False, False, False, Fal...
3 [False, False, False, False, False, False, Fal...
4 [False, True, True, True, True, True, True, Tr...
5 [False, False, False, False, True, True, True,...
6 [False, False, False, False, False, False, Tru...
7 [False, False, False, False, False, False, Fal...
8 [False, False, False, False, False, True, True...
9 [False, False, False, False, False, True, True...
dtype: object
注意:仅测试数组的一个样本就足够了,您无需重复此数组以匹配序列对象的形状。可能类似于
df.iloc[:10]['occ'].values[np.newaxis,:]
工作。这种广播不应该工作,尾随尺寸必须匹配,或者其中一个必须为1:。转置或手动添加新轴。
0 [False, False, False, False, False, False, Tru...
1 [False, True, True, True, True, True, True, Tr...
2 [False, False, False, False, False, False, Fal...
3 [False, False, False, False, False, False, Fal...
4 [False, True, True, True, True, True, True, Tr...
5 [False, False, False, False, True, True, True,...
6 [False, False, False, False, False, False, Tru...
7 [False, False, False, False, False, False, Fal...
8 [False, False, False, False, False, True, True...
9 [False, False, False, False, False, True, True...
dtype: object