Home > Net >  Convert a list in a pandas df column into a numpy array
Convert a list in a pandas df column into a numpy array

Time:02-08

I have a pandas column with a list of lists.

df[0]  
[[[11305.45, 4840.39],  
[11298.25, 4842.75],  
[11292.25, 4846.94],  
[11287.77, 4852.81],  
[11286.58, 4860.15],  
[11305.45, 4840.39]]]

I would like to turn the column into an array.

pd.DataFrame(df[0].values.tolist()).

But I get

AttributeError: 'list' object has no attribute 'values'

The desired output is:

array([[11305.45, 4840.39],  
  [11298.25, 4842.75],  
  [11292.25, 4846.94],  
  [11287.77, 4852.81],  
  [11286.58, 4860.15],  
  [11305.45, 4840.39]])

Any help greatly appreciated.

CodePudding user response:

I think you just need to do it directly because df[0] is already a list.

np.array(df[0])

That is if 0 is a column and not and index of your DataFrame (using numbers for columns can be misleading)

  •  Tags:  
  • Related