How can I normalize the data in a range of columns in my pandas dataframe

前端 未结 4 1217
执念已碎
执念已碎 2021-02-01 04:56

Suppose I have a pandas data frame surveyData:

I want to normalize the data in each column by performing:

surveyData_norm = (surveyData - surveyData.mean         


        
相关标签:
4条回答
  • 2021-02-01 05:20
    import pandas as pd
    import numpy as np
    # let Dataset here be your data#
    
    from sklearn.preprocessing import MinMaxScaler
    minmax = MinMaxScaler()
    
    for x in dataset.columns[dataset.dtypes == 'int64']:
        Dataset[x] = minmax.fit_transform(np.array(Dataset[I]).reshape(-1,1))
    
    0 讨论(0)
  • 2021-02-01 05:22

    I think it's better to use 'sklearn.preprocessing' in this case which can give us much more scaling options. The way of doing that in your case when using StandardScaler would be:

    from sklearn.preprocessing import StandardScaler
    cols_to_norm = ['Age','Height']
    surveyData[cols_to_norm] = StandardScaler().fit_transform(surveyData[cols_to_norm])
    
    0 讨论(0)
  • 2021-02-01 05:29

    You can perform operations on a sub set of rows or columns in pandas in a number of ways. One useful way is indexing:

    # Assuming same lines from your example
    cols_to_norm = ['Age','Height']
    survey_data[cols_to_norm] = survey_data[cols_to_norm].apply(lambda x: (x - x.min()) / (x.max() - x.min()))
    

    This will apply it to only the columns you desire and assign the result back to those columns. Alternatively you could set them to new, normalized columns and keep the originals if you want.

    .....

    0 讨论(0)
  • 2021-02-01 05:33

    Simple way and way more efficient:
    Pre-calculate the mean:
    dropna() avoid missing data.

    mean_age = survey_data.Age.dropna().mean()
    max_age = survey_data.Age.dropna().max()
    min_age = survey_data.Age.dropna().min()
    
    dataframe['Age'] = dataframe['Age'].apply(lambda x: (x - mean_age ) / (max_age -min_age ))
    

    this way will work...

    0 讨论(0)
提交回复
热议问题