Python: Scaling numbers column by column with pandas

后端 未结 6 948
不知归路
不知归路 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:36

    This is not very elegant but the following works for this two column case:

    #Create dataframe
    df = pd.DataFrame({'A':[14,90,90,96,91], 'B':[103,107,110,114,114]})
    
    #Apply operates on each row or column with the lambda function
    #axis = 0 -> act on columns, axis = 1 act on rows
    #x is a variable for the whole row or column
    #This line will scale minimum = 0 and maximum = 1 for each column
    df2 = df.apply(lambda x:(x.astype(float) - min(x))/(max(x)-min(x)), axis = 0)
    
    #Want to now invert the order on column 'B'
    #Use apply function again, reverse numbers in column, select column 'B' only and 
    #reassign to column 'B' of original dataframe
    df2['B'] = df2.apply(lambda x: 1-x, axis = 1)['B']
    

    If I find a more elegant way (for example, using the column index: (0 or 1)mod 2 - 1 to select the sign in the apply operation so it can be done with just one apply command, I'll let you know.

提交回复
热议问题