4

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 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

4

you could do

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

2 Comments

I am running your code with Pandas 1.0.0. and I get NAN. Could you check?
my bad, I didn't specify the axis used to compute the sum, thanks for pointing that out

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.