pandas 对每一列数据进行标准化的方法

yipeiwu_com7年前Python基础

两种方式

>>> import numpy as np 
>>> import pandas as pd 
Backend TkAgg is interactive backend. Turning interactive mode on. 
>>> np.random.seed(1) 
>>> df_test = pd.DataFrame(np.random.randn(4,4)* 4 + 3) 
>>> df_test 
   0   1   2   3 
0 9.497381 0.552974 0.887313 -1.291874 
1 6.461631 -6.206155 9.979247 -0.044828 
2 4.276156 2.002518 8.848432 -5.240563 
3 1.710331 1.463783 7.535078 -1.399565 
>>> df_test_1 = df_test 
>>> df_test.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x))) #方法一 
   0   1   2   3 
0 1.000000 0.823413 0.000000 0.759986 
1 0.610154 0.000000 1.000000 1.000000 
2 0.329499 1.000000 0.875624 0.000000 
3 0.000000 0.934370 0.731172 0.739260 
 
>>> (df_test_1 - df_test_1.min()) / (df_test_1.max() - df_test_1.min())#方法二 
   0   1   2   3 
0 1.000000 0.823413 0.000000 0.759986 
1 0.610154 0.000000 1.000000 1.000000 
2 0.329499 1.000000 0.875624 0.000000 
3 0.000000 0.934370 0.731172 0.739260 

结果一致且正确

以上这篇pandas 对每一列数据进行标准化的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

浅谈function(函数)中的动态参数

我们可向函数传递动态参数,*args,**kwargs,首先我们来看*args,示例如下: 1.show(*args) def show(*args): print(args,typ...

python实现文本文件合并

python合并文本文件示例代码。 python实现两个文本合并 employee文件中记录了工号和姓名 cat employee.txt: 100 Jason Smith 20...

使用pycharm生成代码模板的实例

通过在File->setting->File and Code Templates设置模板代码,这样就可以在新建python文件的时候自动带上抬头。 # -*- codi...

pytorch 指定gpu训练与多gpu并行训练示例

一. 指定一个gpu训练的两种方法: 1.代码中指定 import torch torch.cuda.set_device(id) 2.终端中指定 CUDA_VISIBLE_DEV...

深入理解Python3中的http.client模块

http 模块简介 Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块。 http.client 是一个底层的 HTTP 协议客户端,被更高层的 urlli...