【发布时间】:2022-02-20 06:40:29
【问题描述】:
我正在使用具有分类和数字列的 DataFrame。我正在尝试缩放数字列,因为数字非常极端。我尝试使用下面的代码,但它不断出现错误。我只指出了要应用的数字列。
from sklearn.preprocessing import StandardScaler
numerical = df.iloc[:, 6:24]
scaler = StandardScaler()
scaled_df = scaler.fit_transform(df[numerical])
错误是 ValueError:条件需要的布尔数组,而不是 float64
我不明白是什么原因造成的。我已尝试更新我正在使用的 pandas 版本,但没有修复它。
【问题讨论】:
-
定义
numerical的行实际上将返回DataFrame。所以检查type(numerical)。如果你运行scaled_df = scaler.fit_transform(numerical),它可能会起作用。 -
另外,如果您不想硬编码数字列的位置,您可以使用
df.select_dtypes(include=[np.number])(如果您使用别名np导入了numpy)。