Search code examples
pythonpandasrowdivision

pandas dividing rows by its total


I have this df:

Name    num1  num2   num3  
A       1      2      3    
B       4      5      6    
C       7      8      9    

My goal is to divide each row by the total. This is what I came up with:

df.loc[:,"num1":"num3"] = df.loc[:,"num1":"num3"].div(df["total"], axis=0)

It works well. However, if there are more "numx" columns added in after num3, I would have to manually update the code to "num1":"numx". Is there a way to work around this?


Solution

  • first select matching columns:

    In [21]: cols = df.columns[df.columns.str.contains('^num\d+')]
    

    then divide elements in those rows by their sum (calculated for each row):

    In [22]: df[cols] = df[cols].div(df[cols].sum(axis=1), axis=0)
    
    In [23]: df
    Out[23]:
      Name      num1      num2   num3
    0    A  0.166667  0.333333  0.500
    1    B  0.266667  0.333333  0.400
    2    C  0.291667  0.333333  0.375