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
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.]])
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With