Home > other >  How to convert each row in Pandas DF to 2D numpy array?
How to convert each row in Pandas DF to 2D numpy array?

Time:07-19

I have a Pandas dataframe with 785 columns (784 columns and 1 column as a label).

I wanna use CNN to train a model on it, therfore I should convert each row if the dataframe to "image": (28x28x1) (28x28=784), so a new dataframe will contain only 2 columns.

How can I can it?

The idead is on the picture: enter image description here

CodePudding user response:

Following code is an example of 4 columns and a label.

import numpy as np
import pandas as pd

csv_path = './example.xlsx'

df = pd.read_excel(csv_path)
df_new = pd.DataFrame(columns=['Column', 'label'])

for i, row in df.iterrows():
    a = []
    for j, column in row.iteritems():
        if j == 'label':
            label = column
            break
        a.append(column)
    img = np.array(a).reshape(2, 2)
    label = column
    df_new.loc[i] = [img, label]

df_new.to_csv('result.csv', index=False)

enter image description here

CodePudding user response:

X_train = X_train.values.reshape(-1,28,28,1)

  • Related