convert Dataframe to 2d Array
Asked Answered
W

2

12

I have a data-frame of size (140000,22) dimensions.

i have to create 2d array of equal dimensions to pass it into convolution neural network .

Can you please guide how to transform on this dataframe

Wagonage answered 6/2, 2019 at 8:25 Comment(2)
Possible duplicate of Convert pandas dataframe to NumPy arraySkiascope
please read stackoverflow.com/help/mcveKab
D
16

Edit: If by 'equal' dimensions you mean it should be a square array, this is not possible, since 140000*22 is not a square number. However, in general you don't need a square array to feed data into a CNN, but this of course depends on your specific CNN.

Old answer: You just need to call .values on the DataFrame.

If for example your dataframe is called df, then you can pass df.values to your convolutional neural network.

Dillon answered 6/2, 2019 at 10:16 Comment(5)
Hi but the .values would give us a single array of whole dataframe rightWagonage
Yes, you are right. Sorry I didn't see that you asked for equal dimensions. What dimensions do you want the array to have?Dillon
it should be 2D array of Equal dimensions so that i can fit it for a neural networkWagonage
This is not the right answer at all : see the other commentsIda
A code example would be welcome here!Stites
E
4

Use arr = df.to_numpy()

See: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_numpy.html

It will create a 2d array where each row is the inner array.

[[row1],
 [row2],
 [row3]...
 [row n]]

To turn it into a 2d array where each column is the inner array, use transpose:

arr = np.transpose(arr)

[[all_data_in_col_1],
 [all_data_in_col_2],...
 [all_data_in_col_n]]
Eatmon answered 1/3, 2021 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.