0

I know this question has been asked several times, but I tried all the anwers and still don't get the right result. I just want to do an element-wise multiplication of two pandas dataframes, but it always results in messing up the structure of the matrix:

x = pd.DataFrame([1,1,1],[2,2,2])
y= pd.DataFrame([0,0,0],[1,1,1])

z= x*y should result in z being

2 0
2 0
2 0

But instead results in z being:

0
1   NaN
1   NaN
1   NaN
2   NaN
2   NaN
2   NaN

What am I doing wrong? I tried pandas.mul and pandas.multiply, but no success.

solimanelefant
  • 546
  • 1
  • 5
  • 17
  • 1
    You can't multiply two dataframes using x * y, only series or np.arrays. x.values * y.values gives the correct result, but as an np.array. This np.array you can write in a new column (or in a new dataframe). – above_c_level Jun 22 '20 at 17:36
  • Does this answer your question? [Pandas: Elementwise multiplication of two dataframes](https://stackoverflow.com/questions/21022865/pandas-elementwise-multiplication-of-two-dataframes) – DV82XL Jun 22 '20 at 17:37

1 Answers1

3

You should use: print(x*y.values) instead of print(x*y)

JaniniRami
  • 493
  • 3
  • 11