首页 > 解决方案 > 在特定列规范化 Pandas DataFrame

问题描述

我有一个具有以下结构的 Pandas DataFrame。

Feature 1  | Feature 2  | Feature 3
10         | 200        | True
30         | 233        | False
45         | 344        | True

知道如何仅对功能 1功能 2进行规范化吗?不改变原始 DataFrame 的索引。

我已经尝试了以下代码,但它对所有列进行了规范化并将数据框的索引更改为 0,1,2

x = df.values
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
dataset = pd.DataFrame(x_scaled)

标签: pythonpandasnumpyscikit-learndata-science

解决方案


只需创建数据框的视图:

x = df[['Feature 1', 'Feature 2']]
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(x)
dataset = pd.DataFrame(x_scaled)
dataset['Feature 3'] = df['Feature 3']

推荐阅读