Amelio Vazquez-Reina Amelio Vazquez-Reina - 3 days ago 7
Python Question

Get dot-product of dataframe with vector, and return dataframe, in Pandas

I am unable to find the entry on the method

dot()
in the official documentation. However the method is there and I can use it. Why is this?

On this topic, is there a way compute an element-wise multiplication of every row in a data frame with another vector? (and obtain a
dataframe
back?), i.e. similar to
dot()
but rather than computing the dot product, one computes the element-wise product.

Answer

Here is an example of how to multiply a DataFrame by a vector:

In [60]: df = pd.DataFrame({'A': [1., 1., 1., 2., 2., 2.], 'B': np.arange(1., 7.)})

In [61]: vector = np.array([2,2,2,3,3,3])

In [62]: df.mul(vector, axis=0)
Out[62]: 
   A   B
0  2   2
1  2   4
2  2   6
3  6  12
4  6  15
5  6  18
Comments