Apply function rowwise to pandas dataframe while referencing a column

Viewed 270

I have a pandas dataframe like this:

df = pd.DataFrame({'A': [2, 3], 'B': [1, 2], 'C': [0, 1], 'D': [1, 0], 'total': [4, 6]})

   A  B  C  D  total
0  2  1  0  1      4
1  3  2  1  0      6

I'm trying to perform a rowwise calculation and create a new column with the result. The calculation is to divide each column ABCD by the total, square it, and sum it up rowwise. This should be the result (0 if total is 0):

   A  B  C  D  total  result
0  2  1  0  1      4   0.375
1  3  2  1  0      6   0.389

This is what I've tried so far, but it always returns 0:

df['result'] = df[['A', 'B', 'C', 'D']].apply(lambda x: ((x/df['total'])**2).sum(), axis=1)

I guess the problem is df['total'] in the lambda function, because if I replace this by a number it works fine. I don't know how to work around this though. Appreciate any suggestions.

2 Answers

A combination of div, pow and sum can solve this :

df["result"] = df.filter(regex="[^total]").div(df.total, axis=0).pow(2).sum(1)
df

A   B   C   D   total   result
0   2   1   0   1   4   0.375000
1   3   2   1   0   6   0.388889

you could do

df['result'] = (df.loc[:, "A": 'D'].divide(df.total, axis=0) ** 2).sum(axis=1)
Related