How to normalize dataframe based on a column weight

I have this dataframe, and i want to normalize/standarlize it (columns B,C,D) using column A as weight.

A B C D
34 5 1 12
26 9 0 2
10 0 4 1

Is that possible?

1 answer

  • answered 2021-07-27 15:51 jros

    It sounds like you would like to divide the the values in columns B, C, and D by the corresponding row value in column A.

    To do this with a pandas dataframe called df:

    print(df)
    
    A   B   C   D
    34  5   1   12
    26  9   0   2
    10  0   4   1
    
    cols = df.columns[1:]
    for column in cols:
        df[column] = df[column]/df["A"]
    
    print(df)
    
    A   B           C           D
    34  0.147059    0.029412    0.352941
    26  0.346154    0.000000    0.076923
    10  0.000000    0.400000    0.100000
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum