Luis Henrique
Luis Henrique

Reputation: 771

Convert dataframe 2D in 1D

How convert the dataframe bellow:

            m1,d1         m2,d2         m3,d3         m4,d4  ... 20-40  40-60  60-80  80-100
0    [0.986, 1.0]  [1.0, 0.707]  [0.986, 0.0]  [0.972, 0.5]  ...     0      0  0.655     1.0
1      [1.0, 0.0]  [0.958, 0.0]  [1.0, 0.516]  [0.972, 0.5]  ...     0      0  0.724     1.0
2  [0.958, 0.447]  [0.972, 1.0]  [1.0, 0.516]  [0.901, 1.0]  ...     0      0  0.724     1.0
3  [0.972, 0.894]  [0.972, 1.0]  [0.928, 1.0]  [1.0, 0.354]  ...     0      0  1.000     0.0

In array 1d bellow:

            m1,d1         m2,d2         m3,d3         m4,d4     ... 20-40  40-60  60-80  80-100
0         0.986, 1.0   1.0, 0.707    0.986, 0.0     0.972, 0.5  ...     0    0    0.655    1.0

Output:

[4 rows x 10 columns]
[list([0.986, 1.0]) list([1.0, 0.0]) list([0.958, 0.447])
 list([0.972, 0.894]) list([1.0, 0.707]) list([0.958, 0.0])
 list([0.972, 1.0]) list([0.972, 1.0]) list([0.986, 0.0])
 list([1.0, 0.516]) list([1.0, 0.516]) list([0.928, 1.0])
 list([0.972, 0.5]) list([0.972, 0.5]) list([0.901, 1.0])
 list([1.0, 0.354]) list([0.862, 0.408]) list([0.812, 1.0])
 list([0.9, 0.204]) list([1.0, 0.816]) 0 0 0 0 0 0 0 0 0 0 0 0 0.655 0.724
 0.724 1.0 1.0 1.0 1.0 0.0]

Script:

import pandas as pd

Ninput = [[[0.986, 1.0], [1.0, 0.707], [0.986, 0.0], [0.972, 0.5], [0.862, 0.408]], [[1.0, 0.0], [0.958, 0.0], [1.0, 0.516], [0.972, 0.5], [0.812, 1.0]], [[0.958, 0.447], [0.972, 1.0], [1.0, 0.516], [0.901, 1.0], [0.9, 0.204]], [[0.972, 0.894], [0.972, 1.0], [0.928, 1.0], [1.0, 0.354], [1.0, 0.816]]]

Noutput = [[0, 0, 0, 0.655, 1.0], [0, 0, 0, 0.724, 1.0], [0, 0, 0, 0.724, 1.0], [0, 0, 0, 1.0, 0.0]]

def conversionDataframe(dataNeuronInput,dataNeuronOutput):
    """ Converts data to a dataframe pandas """
    ni = pd.DataFrame(data= dataNeuronInput)
    ni.columns = ['m1,d1', 'm2,d2', 'm3,d3', 'm4,d4', 'm5,d5']

    no = pd.DataFrame(data= dataNeuronOutput)
    no.columns = ['0-20', '20-40', '40-60', '60-80', '80-100']

    return pd.concat([ni, no], axis=1)

dataFrameNoBinary = conversionDataframe(Ninput, Noutput)

print(dataFrameNoBinary.values.ravel('F'))

Note: Goals is to insert at neuron layer for training using classification binary with keras (Neural Network)

Upvotes: 1

Views: 200

Answers (1)

Nazmul Hasan
Nazmul Hasan

Reputation: 940

You can do this using numpy very easily.

np.hstack(Ninput)

Upvotes: 1

Related Questions