Skip to content Skip to sidebar Skip to footer

Conversion Of Lists Into A Numpy Array In Pandas Dataframe

We have a dataframe where the elements of one column are lists (the discussion is not about if this should be done or not). A simple example is the following: df = pd.DataFrame([[1

Solution 1:

If always same length of lists is possible create nested lists and then convert to np.array:

arr = np.array(df['B'].values.tolist())
#alternative
#arr = np.array(df['B'].tolist())
print (arr)
[[ 123  234  234]
 [ 124   25  235]
 [1267  267 2345]]

Post a Comment for "Conversion Of Lists Into A Numpy Array In Pandas Dataframe"