Maged
Maged

Reputation: 998

Product Sum for two rows from two dataframes and add sum to a new column

I have 2 df as Main df

main_df=pd.DataFrame(np.arange(3,9).reshape(-1,2), columns = list('AB'))

    A   B
0   3   4
1   5   6
2   7   8

weights df

w_df=pd.DataFrame(np.arange(1,5).reshape(-1,2), columns = ['wA','wB'])/10

    wA  wB
0   0.1 0.2
1   0.3 0.4

I need to multiply specific row from weights df into all columns of main df and put the summation into a column in main df so output should be like in case of using 1st row from weights df

Prod Sum of 1st row in main = 0.1 * 3 + 0.2 *4 = 1.1

Prod Sum of 2nd row in main = 0.1 * 5 + 0.2 *6 = 1.7

Prod Sum of 3rd row in main = 0.1 * 7 + 0.2 *8 = 2.3

i.e. output of main df should be like (in case of using 1st row from weights df)

    A   B   prod_sum
0   3   4   1.1
1   5   6   1.7
2   7   8   2.3

Upvotes: 0

Views: 501

Answers (3)

Mykola Zotko
Mykola Zotko

Reputation: 17794

You need a dot product:

main_df.dot(w_df.iloc[0].values)

or

main_df @ w_df.iloc[0].values

Output:

0    1.1
1    1.7
2    2.3

Upvotes: 1

Nicholas Polino
Nicholas Polino

Reputation: 95

You could do something like:

main_df['product1'] = main_df['A'] * w_df.loc[0, 'wA] Repeat for product2/B/wB

Then

main_df['sum'] = main_df['product1'] + main_df['product2']

Hope this helps, certainly a better way. Cheers.

Upvotes: 0

sammywemmy
sammywemmy

Reputation: 28644

Since you are interested in just the first row of w_df, you select just that row, multiply main_df with it, using the mul function, along the row axis; after the product computation, you can them sum along the axis.

main_df.assign(prod_sum=main_df.mul(w_df.iloc[0].array, axis=1).sum(1))

    A   B   prod_sum
0   3   4   1.1
1   5   6   1.7
2   7   8   2.3

Upvotes: 2

Related Questions