Search code examples
pythonpandassumrowaverage

How to get the proportion of values in a row in pandas?


Let's say I have the following df:

col1|col2|col3
  1 |  3 |  1
  2 |  2 |  1

and I would like to have the proportion of values for rows, so stg like this:

col1|col2|col3
 0.2| 0.6| 0.2
 0.4| 0.4| 0.4

so far, my main problem is how to grab the sum of the rows:

mydf["col1_proportion"] = mydf["col1"].apply(lambda x: x / (XXX)  )  

where (XXX) should be the sum for the given row


Solution

  • Try div with sum

    out = df.div(df.sum(1),axis=0)
    Out[36]: 
       col1  col2  col3
    0   0.2   0.6   0.2
    1   0.4   0.4   0.2