Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply function rowwise to pandas dataframe while referencing a column

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.

like image 974
Wouter Avatar asked Jan 25 '23 19:01

Wouter


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
like image 199
sammywemmy Avatar answered Jan 27 '23 09:01

sammywemmy


you could do

df['result'] = (df.loc[:, "A": 'D'].divide(df.total, axis=0) ** 2).sum(axis=1)
like image 30
Ayoub ZAROU Avatar answered Jan 27 '23 09:01

Ayoub ZAROU