0

I have a list of dataframes which looks like shown in image.

The list length is 200 where it has 200 dataframes, each dataframe has length of 205 and has 6 columns.

I want to bring this list in the array of shape (200,205,6)

I tried it doing follwing method but I keep getting the error given below.

np.array(samples).reshape(200,205,6)

For this code I am getting an error:

ValueError: cannot copy sequence with size 205 to array axis with dimension 1

I also removed 5 columns and kept only one column in all datarames but yet am getting same error.

Is there any way to bring it in the required shape..enter image description here

Roshan
  • 664
  • 5
  • 23
Bhakti
  • 21
  • 4
  • still not very clear what you want to do - do you want to take all the values in the dataframes and create a nested array from the dataframes? – Chinny84 Jun 25 '19 at 14:44

1 Answers1

0

You're getting that error because numpy doesn't know how to convert a pandas dataframe into an np.array, but you can solve this pretty quickly:

np.array([df.values for for df in list_of_dataframes])

I tried it with this sample data:

data = [pd.DataFrame(np.random.random((205,6))) for i in range(200)]

np.array([df.values for df in data]).shape

(200, 205, 6)

PS: Possibly a duplicate of: Convert pandas dataframe to NumPy array

ernest
  • 161
  • 5