How to use sklearn fit_transform with pandas and return dataframe instead of numpy array?

后端 未结 5 1990
陌清茗
陌清茗 2020-12-04 08:38

I want to apply scaling (using StandardScaler() from sklearn.preprocessing) to a pandas dataframe. The following code returns a numpy array, so I lose all the column names a

5条回答
  •  被撕碎了的回忆
    2020-12-04 09:07

    import pandas as pd    
    from sklearn.preprocessing import StandardScaler
    
    df = pd.read_csv('your file here')
    ss = StandardScaler()
    df_scaled = pd.DataFrame(ss.fit_transform(df),columns = df.columns)
    

    The df_scaled will be the 'same' dataframe, only now with the scaled values

提交回复
热议问题