gpt4 book ai didi

python - Pandas 系列到二维数组

转载 作者:太空宇宙 更新时间:2023-11-03 10:51:47 25 4
gpt4 key购买 nike

因此,我使用了 Put a 2d Array into a Pandas Series 中的答案将 2D numpy 数组放入 pandas 系列。简而言之,就是

a = np.zeros((5,2))
s = pd.Series(list(a))

现在,将 pandas 系列转换回二维数组的最便宜的方法是什么?如果我尝试 s.values,我会得到具有 object dtype 的数组数组。

到目前为止,我尝试了 np.vstack(s.values),但它当然会复制数据。

最佳答案

我相信你需要:

a = np.array(s.values.tolist())
print (a)
[[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]
[ 0. 0.]]

a = np.zeros((50000,2))
s = pd.Series(list(a))

In [131]: %timeit (np.vstack(s.values))
10 loops, best of 3: 107 ms per loop

In [132]: %timeit (np.array(s.values.tolist()))
10 loops, best of 3: 19.7 ms per loop

In [133]: %timeit (np.array(s.tolist()))
100 loops, best of 3: 19.6 ms per loop

但是如果转置差异很小(但是 caching ):

a = np.zeros((2,50000))
s = pd.Series(list(a))
#print (s)

In [159]: %timeit (np.vstack(s.values))
The slowest run took 23.31 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 55.7 µs per loop

In [160]: %timeit (np.array(s.values.tolist()))
The slowest run took 7.20 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 49.8 µs per loop

In [161]: %timeit (np.array(s.tolist()))
The slowest run took 7.31 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 62.6 µs per loop

关于python - Pandas 系列到二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48823400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com