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

前端 未结 4 1216
执念已碎
执念已碎 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: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...

提交回复
热议问题