Search code examples
arrayspandasdataframenormalize

Python/Pandas: Array (back) to DataFrame


I applied following function to normalize the columns in my dataframe.

from sklearn.preprocessing import normalize

pd.DataFrame(normalize(traffic, norm='l2', axis=1, copy=True, return_norm=False))

However, this function returns an array

array([[ 0.19781966,  0.21981735,  0.        , ...,  0.05655909,
         0.        ,  0.00033033],
       [ 0.18050277,  0.2031944 ,  0.        , ...,  0.15848418,
         0.        ,  0.00032616],
       [ 0.14110768,  0.16995336,  0.        , ...,  0.0820779 ,
         0.        ,  0.00023619],

Is there any way to write the normalized data into the original "traffic" dataframe and replace the raw values?

I realized that when I apply

pd.DataFrame(normalize(traffic, norm='l2', axis=1, copy=True, return_norm=False))

all original column names and the index are gone.


Solution

  • If normalize function returns an array of the same shape as the traffic DF you can do it this way:

    pd.DataFrame(normalize(traffic, norm='l2', axis=1, copy=True, return_norm=False),
                 columns=traffic.columns, index=traffic.index)