I want to multiply two data frames to get a new one. All variables in both data frames are integers, and indexes in both are reset and go from 0:n. These are my data
df1
11 12 13 14 15 16 17 18 19 20 0 54 87 77 53 30 21 0 0 0 0 1 60 146 171 66 36 34 0 0 0 0 2 49 76 87 57 34 35 0 0 0 0 3 49 76 77 53 30 21 0 0 0 0
df2
A5 A6 A7 A8 0 200 300 400 500 1 201 301 401 501 2 202 302 402 502 3 203 303 403 503
If I use .mul
method, all results in the data frame are NaN and the shape is all wrong (no matter which axis I use).
df2.mul(df1)
Result
11 12 13 14 15 16 17 18 19 20 A5 A6 A7 A8 0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
Expected result is a table like this
11 12 13 14 15 16 17 18 19 20 72900 129600 140000 79700 45400 38900 0 0 0 0 73112 129985 140412 79929 45530 39011 0 0 0 0 73324 130370 140824 80158 45660 39122 0 0 0 0 73536 130755 141236 80387 45790 39233 0 0 0 0
If I multiply df1 or df2 with scalar (like df1 * 2
) the results are ok.
How do I solve this?
CodePudding user response:
This doesn't return the columns with zeros but this will return the two multiplied together. Is this what you are looking for?
df1.drop(columns=df1.columns[len(df2.columns):]).mul(df2)
CodePudding user response:
So, .mul
is for element wise multiplication, but I wanted to do matrix multiplication, so .dot
was needed. The solution is:
df1.set_index(df2.columns, inplace = True)
df2.dot(df1)