How to normalize only continuous variables in dataframe?

Viewed 168

I'm trying to normalize the continuous variables from my dataframe but I'm getting the following error

ValueError: could not convert string to float: 'M'

I know that the error occurs because I have some categorical variables so how can I ignore the categorical variables. This is my code

X=data
mms=MinMaxScaler()
mms.fit(X)
Xnorm=mms.transform(X)
print(Xnorm.min(axis=0))
print(Xnorm.max(axis=0))
print(Xnorm.shape)
1 Answers

Select the numerical columns, then apply min-max transformation and store the result back into selected numerical columns

X = data.select_dtypes(np.number)
data[X.columns] = MinMaxScaler().fit_transform(X)
Related