Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum of multiplication of cells in the same row but different column for pandas data frame

Tags:

python

pandas

I have a data frame

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

My data looks like this

Index   A   B
0       1   2
1       2   3
2       3   4

I would like to calculate the sum of multiplication between A and B in each row. The expected result should be (1x2)+(2x3)+(3x4) = 2 + 6 + 12 = 20. May I know the pythonic way to do this instead of looping?

like image 530
Ratchainant Thammasudjarit Avatar asked Mar 07 '16 09:03

Ratchainant Thammasudjarit


1 Answers

You can try multiple columns A and B and then use sum :

import pandas as pd
import numpy as np

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

print df['A'] * df['B']
0     2
1     6
2    12
dtype: int64

print (df['A'] * df['B']).sum()
20

Or use prod for multiple all columns:

print df.prod(axis=1)
0     2
1     6
2    12
dtype: int64

print df.prod(axis=1).sum()
20

Thank you ajcr for comment:

If you have just two columns, you can also use df.A.dot(df.B) for extra speed, but for three or more columns this is the way to do it!

like image 154
jezrael Avatar answered Oct 16 '22 06:10

jezrael