Matrices are not aligned while dot product with DataFrames

butter_prices
    Almond butter   Peanut butter   Cashew butter
Price   10  8   12
weekly_sales
    Almonds butter  Peanut butter   Cashew butter
Mon 12  15  0
Tues    3   3   7
Wed 9   19  18
Thurs   4   6   12
Fri 1   6   7

Now, while doing

butter_prices.shape, weekly_sales.shape
((1, 3), (5, 3))

now the error shows while calculating

butter_prices.dot(weekly_sales.T)

ValueError: matrices are not aligned

Expected the Dot product of two

  • 1

    What’s your expected outpu?

    – 

  • does weekly_sales @ butter_prices.T give the right output?

    – 

Pass the values

out = weekly_sales.dot(butter_prices.iloc[0].values)
out
Mon      240
Tues     138
Wed      458
Thurs    232
Fri      142
dtype: int64

Or align the name

butter_prices.columns = weekly_sales.columns
butter_prices.dot(weekly_sales.T)
       Mon  Tues  Wed  Thurs  Fri
Price  240   138  458    232  142

You have typo in the column name Almonds butter -> Almond butter:

weekly_sales = weekly_sales.rename(columns={"Almonds butter": "Almond butter"})
print(butter_prices.dot(weekly_sales.T))

Prints:

       Mon  Tues  Wed  Thurs  Fri
Price  240   138  458    232  142

Leave a Comment