Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert one column in dataframe into a 2D array in python

I have an dataframe which contain the observed data as:

import pandas as pd
d = {'ID': [0,1,2], 'Value': 
[[1,2,1],[5,4,6],[7,20,9]]}
df = pd.DataFrame(data=d)

how can I get an array from the value to form a 2D numpy.ndarray

 [[1, 2, 1],
 [5, 4, 6],
 [7, 20, 9]]

with shape:(3,3)

I try

print(df['Value'].values)

but it gives me

[list([1, 2, 1]) list([5, 4, 6]) list([7, 20, 9])]

which is not what I want

like image 883
jason Avatar asked Apr 13 '19 01:04

jason


1 Answers

You can extract the column lists, then array-ify using a couple of methods below.

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

array([[ 1,  2,  1],
       [ 5,  4,  6],
       [ 7, 20,  9]])

# np.vstack(df['Value'])
np.stack(df['Value'])

array([[ 1,  2,  1],
       [ 5,  4,  6],
       [ 7, 20,  9]])

If the lists are un-evenly sized, this will return a regular 2D array with nans in missing positions.

df['Value'] = [[1, 2], [3], [4, 5, 6]]
df

   ID      Value
0   0     [1, 2]
1   1        [3]
2   2  [4, 5, 6]

# pd.DataFrame(df['Value'].tolist()).values   #  < v0.24
pd.DataFrame(df['Value'].tolist()).to_numpy() #  v0.24+

array([[ 1.,  2., nan],
       [ 3., nan, nan],
       [ 4.,  5.,  6.]])
like image 83
cs95 Avatar answered Nov 04 '22 03:11

cs95