'Getting an error when applying sklearn StandardScaler on Python

I'm working with a DataFrame that has categorical and numerical columns. I am trying to scale the numerical columns as the numbers are pretty extreme. I tried to use the code below but it keeps getting an error. I indicated only the numerical columns to be applied.

from sklearn.preprocessing import StandardScaler

numerical = df.iloc[:, 6:24]
scaler = StandardScaler()
scaled_df = scaler.fit_transform(df[numerical])

The error is ValueError: Boolean array expected for the condition, not float64

I don't understand what is causing it. I have tried updating the pandas version that I am using and that did not fix it.



Solution 1:[1]

numerical is already a dataframe (a subset of df), so df[numerical] does not make sense here:

scaled_df = scaler.fit_transform(numerical)

If you try only:

>>> df[numerical]
...
ValueError: Boolean array expected for the condition, not float64

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Corralien