Python:用 pandas 逐列缩放数字 [英] Python: Scaling numbers column by column with pandas
本文介绍了Python:用 pandas 逐列缩放数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据框'df',我想在其中逐列执行一些缩放.
I have a Pandas data frame 'df' in which I'd like to perform some scalings column by column.
- 在"a"列中,我需要将最大值设为1,将最小值设为0,并相应地分配所有其他值.
- 但是,在"b"列中,我需要最小数为1 ,最大数为0 ,并相应地分配所有其他值.
- In column 'a', I need the maximum number to be 1, the minimum number to be 0, and all other to be spread accordingly.
- In column 'b', however, I need the minimum number to be 1, the maximum number to be 0, and all other to be spread accordingly.
是否有熊猫函数来执行这两项操作?如果没有的话,numpy当然可以.
Is there a Pandas function to perform these two operations? If not, numpy would certainly do.
a b
A 14 103
B 90 107
C 90 110
D 96 114
E 91 114
推荐答案
您可以减去最小值,然后除以最大值(请注意0/0).请注意,减去最小值后,新的最大值就是原始最大值-最小值.
You could subtract by the min, then divide by the max (beware 0/0). Note that after subtracting the min, the new max is the original max - min.
In [11]: df
Out[11]:
a b
A 14 103
B 90 107
C 90 110
D 96 114
E 91 114
In [12]: df -= df.min() # equivalent to df = df - df.min()
In [13]: df /= df.max() # equivalent to df = df / df.max()
In [14]: df
Out[14]:
a b
A 0.000000 0.000000
B 0.926829 0.363636
C 0.926829 0.636364
D 1.000000 1.000000
E 0.939024 1.000000
要切换列的顺序(从1到0,而不是从0到1):
To switch the order of a column (from 1 to 0 rather than 0 to 1):
In [15]: df['b'] = 1 - df['b']
另一种方法是对 first (df['b'] = -df['b']
)的b列取反.
An alternative method is to negate the b columns first (df['b'] = -df['b']
).
这篇关于Python:用 pandas 逐列缩放数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文