我有两个相同长度的pandas series
,如下所示:
S1 =
0 -0.483415
1 -0.514082
2 -0.515724
3 -0.519375
4 -0.505685
...
S2 =
1 -0.961871
2 -0.964762
3 -0.963798
4 -0.962112
5 -0.962028
...
我想将它们压缩成一个numpy ndarray of ndarray
,使它看起来像这样:
<class 'numpy.ndarray'>
[[-0.483415 -0.961871]
[-0.514082 -0.964762]
[-0.515724 -0.963798]
...
]
如果我想要list
的tuple
,我可以这样说:
v = list(zip(S1, S2))
这给了我:
<class 'list'>
[(-0.48341467662344273, -0.961871075696243),
(-0.5140815458448855, -0.9647615371349125),
...
]
我如何做同样的“拉链”但回到ndarray of ndarray
?我不想要循环。
Zip
在这里没有必要,为了更好的性能使用numpy
或pandas
:
arr = np.hstack((S1.values[:, None], S2.values[:, None]))
要么:
arr = np.vstack((S1, S2)).T
要么:
arr = pd.concat([S1.reset_index(drop=True), S2.reset_index(drop=True)], axis=1).values
要么:
arr = np.c_[S1, S2]
print (arr)
[[-0.483415 -0.961871]
[-0.514082 -0.964762]
[-0.515724 -0.963798]
[-0.519375 -0.962112]
[-0.505685 -0.962028]]
性能:
#50k values
S1 = pd.concat([S1] * 10000, ignore_index=True)
S2 = pd.concat([S2] * 10000, ignore_index=True)
In [107]: %timeit arr = np.hstack((S1.values[:, None], S2.values[:, None]))
133 µs ± 15.9 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [108]: %timeit arr = np.vstack((S1, S2)).T
176 µs ± 12 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [109]: %timeit arr = pd.concat([S1.reset_index(drop=True), S2.reset_index(drop=True)], axis=1).values
1.49 ms ± 74.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [110]: %timeit arr = np.c_[S1, S2]
320 µs ± 10.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [111]: %timeit np.array(list(zip(S1, S2)))
33 ms ± 545 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
把它投到ndarray
:
>>> a = [1,2,3,4]
>>> b = [5,6,7,8]
>>> c = list(zip(a, b))
>>> c
[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> d = np.array(c)
>>> d
array([[1, 5],
[2, 6],
[3, 7],
[4, 8]])
>>> d.shape
(4, 2)
尝试:
numpy.hstack((S1, S2))
我认为它应该有效。