【发布时间】:2017-12-01 06:06:01
【问题描述】:
我正在使用一个函数来获取 3 组数组 (cp1) 的笛卡尔积,这些数组派生自 pandas 数据框中的列。在获得这个(cp2)之后,我运行一个测试来检查总和是否小于 1.05,如果是,我想从原始数组中找到从它为真的位置生成它的组合。有没有办法用 numpy / python / pandas 做到这一点?任何帮助,将不胜感激。最终,我想在生成真实条件的数据框中的每一列中获取原始索引。
In [780]: cp1.shape
Out[780]: (8, 3)
In [781]: cp2.shape
Out[781]: (512, 3)
In [782]: cp2
Out[782]: array([[ 0.43478262, 0.33333334, 0.29411763],
[ 0.43478262, 0.33333334, 0.32258067],
[ 0.43478262, 0.33333334, 0.32786885],
...,
[ 0.43478262, 0.32258067, 0.32258067],
[ 0.43478262, 0.32258067, 0.29850748],
[ 0.43478262, 0.32258067, 0.32258067]])
In [783]: bools = cp2.sum(1) < 1.05
In [784]: np.where(bools)
Out[784]: (array([392, 398, 440, 446]),)
【问题讨论】:
标签: python arrays pandas numpy cartesian-product