9

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

1 Answer 1

8

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.]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.