Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiply two Pandas dataframes with same shape and same columns names

I have two dataframes A, B with NxM shape. I want to multiply both such that each element of A is multiplied with respective element of B.

e.g:
A,B = input dataframes
C = final dataframe
I want C[i][j] = A[i][j]*B[i][j] for i=1..N and j=1..M

I searched but couldn't get exactly the solution.

like image 474
impossible Avatar asked Apr 10 '16 13:04

impossible


People also ask

Can you multiply two DataFrames in pandas?

The mul() method of DataFrame object multiplies the elements of a DataFrame object with another DataFrame object, series or any other Python sequence. mul() does an elementwise multiplication of a DataFrame with another DataFrame, a pandas Series or a Python Sequence.

How do I multiply column values in pandas?

Use the * operator to multiply a column by a constant number Select a column of DataFrame df using syntax df["column_name"] and set it equal to n * df["column_name"] where n is the number to multiply by.

How do I merge two similar DataFrames in pandas?

The concat() function in pandas is used to append either columns or rows from one DataFrame to another. The concat() function does all the heavy lifting of performing concatenation operations along an axis while performing optional set logic (union or intersection) of the indexes (if any) on the other axes.


1 Answers

I think you can use:

C = A * B

Next solution is with mul:

C = A.mul(B)

Sample:

print A
   a  b
0  1  3
1  2  4
2  3  7

print B
   a  b
0  2  3
1  1  4
2  3  2

print A * B
   a   b
0  2   9
1  2  16
2  9  14

print A.mul(B)
   a   b
0  2   9
1  2  16
2  9  14

Timings with lenght of A and B 300k:

In [218]: %timeit A * B
The slowest run took 4.27 times longer than the fastest. This could mean that an intermediate result is being cached 
100 loops, best of 3: 3.57 ms per loop

In [219]: %timeit A.mul(B)
100 loops, best of 3: 3.56 ms per loop

A = pd.concat([A]*100000).reset_index(drop=True)
B = pd.concat([B]*100000).reset_index(drop=True)

print A * B
print A.mul(B)
like image 122
jezrael Avatar answered Nov 02 '22 14:11

jezrael