pandas 对每一列数据进行归一化

两种方式

 

>>> 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


结果一致且正确

 

你可能感兴趣的:(python)