当单元格元素列出时,从pandas列中获取ndarray

时间:2019-04-11 21:01:44

标签: python arrays pandas numpy

我有一个看起来像这样的熊猫数据框:

>>> df = pd.DataFrame({'a': list(range(10))})
>>> df['a'] = df.a.apply(lambda x: x*np.array([1,2,3]))
>>>df.head()
            a
0   [0, 0, 0]
1   [1, 2, 3]
2   [2, 4, 6]
3   [3, 6, 9]
4  [4, 8, 12]

我想从df中获取列a作为ndarray。但是当我这样做时,我得到了一个数组数组

>>> df.a.values
array([array([0, 0, 0]), array([1, 2, 3]), array([2, 4, 6]),
       array([3, 6, 9]), array([ 4,  8, 12]), array([ 5, 10, 15]),
       array([ 6, 12, 18]), array([ 7, 14, 21]), array([ 8, 16, 24]),
       array([ 9, 18, 27])], dtype=object)

如何获取返回的输出为

array([[ 0,  0,  0],
       [ 1,  2,  3],
       [ 2,  4,  6],
       [ 3,  6,  9],
       [ 4,  8, 12],
       # ...
])

2 个答案:

答案 0 :(得分:2)

使用熊猫

df.a.apply(pd.Series).values

使用numpy,

np.vstack(df.a.values)

你得到

array([[ 0,  0,  0],
       [ 1,  2,  3],
       [ 2,  4,  6],
       [ 3,  6,  9],
       [ 4,  8, 12],
       [ 5, 10, 15],
       [ 6, 12, 18],
       [ 7, 14, 21],
       [ 8, 16, 24],
       [ 9, 18, 27]])

答案 1 :(得分:2)

检查

np.array(df['a'].tolist())

array([[ 0,  0,  0],
       [ 1,  2,  3],
       [ 2,  4,  6],
       [ 3,  6,  9],
       [ 4,  8, 12],
       [ 5, 10, 15],
       [ 6, 12, 18],
       [ 7, 14, 21],
       [ 8, 16, 24],
       [ 9, 18, 27]], dtype=int64)