Reputation: 21631
If I have two DataFrames how do I multiply them together by column to produce a DataFrame with the result. For example...
df1 = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=['A', 'B', 'C', 'D'])
A B C D
0 6 9 3 8
1 1 7 9 9
2 6 2 0 8
3 3 6 8 4
4 6 0 4 8
df2 = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=['A', 'B', 'C', 'D'])
A B C D
0 9 8 5 3
1 9 5 6 7
2 6 9 6 3
3 7 6 2 5
4 1 5 2 7
The result would be this.....
A B C D
0 54 72 15 24
1 9 35 54 63
etc..
Upvotes: 0
Views: 2507
Reputation: 353059
[CW; just to take this off the unanswered questions list..]
I think you want df1 * df2
. For example:
>>> df1 = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=['A', 'B', 'C', 'D'])
>>> df2 = pd.DataFrame(np.random.randint(10, size=(5, 4)), columns=['A', 'B', 'C', 'D'])
>>> df1
A B C D
0 0 6 5 7
1 8 6 4 0
2 6 2 4 3
3 8 8 7 6
4 8 7 9 0
[5 rows x 4 columns]
>>> df2
A B C D
0 0 0 3 0
1 8 5 6 5
2 7 4 9 7
3 8 4 4 1
4 2 5 6 4
[5 rows x 4 columns]
>>> df1 * df2
A B C D
0 0 0 15 0
1 64 30 24 0
2 42 8 36 21
3 64 32 28 6
4 16 35 54 0
[5 rows x 4 columns]
Upvotes: 2