Amelio Vazquez-Reina Amelio Vazquez-Reina - 1 year ago 147
Python Question

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

I am unable to find the entry on the method

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
back?), i.e. similar to
but rather than computing the dot product, one computes the element-wise product.

Answer Source

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)
   A   B
0  2   2
1  2   4
2  2   6
3  6  12
4  6  15
5  6  18
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download