Python: Scaling numbers column by column with pandas

后端 未结 6 935
不知归路
不知归路 2020-12-13 08:54

I have a Pandas data frame \'df\' in which I\'d like to perform some scalings column by column.

  • In column \'a\', I need the maximum number to be 1, the minimum
6条回答
  •  庸人自扰
    2020-12-13 09:44

    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
    

    To switch the order of a column (from 1 to 0 rather than 0 to 1):

    In [15]: df['b'] = 1 - df['b']
    

    An alternative method is to negate the b columns first (df['b'] = -df['b']).

提交回复
热议问题