pandas 系列到二维数组 [英] Pandas series to 2d array

查看:82
本文介绍了 pandas 系列到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我使用了 将一个二维数组放入一个Pandas 系列 将 2D numpy 数组放入 Pandas 系列.总之就是

So, I used the answer from Put a 2d Array into a Pandas Series to put 2D numpy array to pandas series. In short, it is

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

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

Now, what is the cheapest way to convert that pandas Series back to 2D array? If I try s.values, I get array of arrays with object dtype.

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

So far I tried np.vstack(s.values) but it copies the data, of course.

推荐答案

我相信您需要:

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

但如果转置差异很小(但缓存):

But if transpose difference is small (but 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

这篇关于 pandas 系列到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆