Home > Back-end >  Pandas Dataframe Matrix Multiplication using @
Pandas Dataframe Matrix Multiplication using @

Time:12-06

I am attempting to perform matrix multiplication between a Pandas DataFrame and a Pandas Series. I've set them up like so:

c = pd.DataFrame({
    "s1":[.04, .018, .0064],
    "s2":[.018, .0225, .0084],
    "s3":[.0064, .0084, .0064],
    })
x = pd.Series([0,0,0], copy = False)

I want to perform x @ c @ x, but I keep getting a ValueError: matrices are not aligned error flag. Am I not setting up my matrices properly? I'm not sure where I am going wrong.

CodePudding user response:

x @ c returns a Series object which has different indices as x. You can use the underlying numpy array to do the calculation:

(x @ c).values @ x.values
# 0.39880000000000004
  • Related