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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With