python - 在特定列规范化 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)
解决方案
只需创建数据框的视图:
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']
推荐阅读
- python - 在字典中对互斥的 ID 进行分组
- r - RStudio 中 tidyverse 启动消息中的编码问题:UTF-8 输出失真()
- sql - 如何在插入语句执行后仅触发一次(多次插入)?
- asp.net-core - 带有 Swagger 的 ASP.NET CORE Web API 生成错误的属性名称
- angular - 在 Observable 中添加一个元素 - Ionic 4
- visual-c++ - 是否可以将 Clang libtooling 与其他编译器一起使用?
- javascript - 从 React 中的 API 数组获取数据
- java - Java中重复数据的反击
- python - 为什么 Scappy 中的 sr1 总是返回 None?
- lua - 从函数中获取多个值而不在 LUA 中创建变量