dataframe数据归一化python
时间: 2023-09-12 09:13:35 浏览: 160
python numpy 按行归一化的实例
要对DataFrame数据进行归一化,可以使用Python中的sklearn库中的preprocessing模块中的MinMaxScaler()方法。以下是实现数据归一化的示例代码:
```
import pandas as pd
from sklearn import preprocessing
def min_max_sca(data_x01, data_type):
if data_type == 'df':
data_x01 = data_x01.values.tolist()
data_x01 = pd.DataFrame(data_x01)
min_max_scaler = preprocessing.MinMaxScaler()
data_x01_Sca = min_max_scaler.fit_transform(data_x01)
data_x01_Sca = data_x01_Sca.reshape(1, -1)[0]
if data_type == 'df':
data_x01_Sca = pd.DataFrame(data_x01_Sca).iloc[:,0]
return data_x01_Sca
lists = [10, 20, 30, 40, 50]
dataCol_i = min_max_sca(lists, 'list')
print(dataCol_i)
dataCol_i = pd.DataFrame(lists)
print(type(dataCol_i), dataCol_i)
dataCol_i = min_max_sca(dataCol_i, 'df')
print(type(dataCol_i), dataCol_i)
```
这段代码会将DataFrame类型的数据进行归一化,输出结果为归一化后的DataFrame数据。 <span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Python编程语言学习:将list或者dataframe类型数据进行归一化并返回(便于绘图)](https://blog.csdn.net/qq_41185868/article/details/121780843)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [python numpy 按行归一化的实例](https://download.csdn.net/download/weixin_38518638/13765166)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文