1

I've written the following Python/Pandas code to multiply each column of an M row x N col dataframe (A) by an M x 1 dataframe (b) to yield the M x N dataframe C:

def multiply_columns(A, b):
    C = pd.DataFrame(A.values * b.values, columns=A.columns, index=b.index)
    return C

In other words, it multiplies each column of a matrix by a column vector of equal length.

The code works fine, but I can't recall the formal name for this operation. Thoughts?

1 Answers1

2

It is called "broadcasting". Please see the numpy documentation on the subject: Broadcasting.

Also, it is important to note that A.values and b.values are not matrices, they are arrays. This may seem like a minor detail, but it is very important. Many mathematical operations on matrices produce completely different results than their corresponding operations on arrays. So, for example, M1*M2 is a matrix product for matrices, while it is an element-by-element multiplication for arrays. See more details in This answer.

Community
  • 1
  • 1
TheBlackCat
  • 9,791
  • 3
  • 24
  • 31