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

yipeiwu_com6年前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设计】。

相关文章

python3.7 openpyxl 删除指定一列或者一行的代码

python3.7 openpyxl 删除指定一列或者一行 # encoding:utf-8 import pandas as pd import openpyxl xl = pd....

python利用hook技术破解https的实例代码

相对于http协议,http是的特点就是他的安全性,http协议的通信内容用普通的嗅探器可以捕捉到,但是https协议的内容嗅探到的是加密后的内容,对我们的利用价值不是很高,所以一些大的...

Django+Ajax+jQuery实现网页动态更新的实例

views.py中的修改 增加相应的请求处理函数: def getdevjson(request): print 'get here' if ('key' in request....

Python 一句话生成字母表的方法

List >>> [chr(i) for i in range(97,123)] ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',...

Python 操作 ElasticSearch的完整代码

Python 操作 ElasticSearch的完整代码

官方文档:https://elasticsearch-py.readthedocs.io/en/master/   1、介绍     python提供了操作ElasticSearch 接...