Get a dot data product with a vector and return the dataframe to Pandas

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

In this section, is there a way to calculate the multiplication by the elements of each row in a data frame with a different vector? (and get a dataframe back?), i.e. similar to dot() , but instead of computing a point product, an element-wise product is computed.

+7
source share
3 answers

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]: AB 0 2 2 1 2 4 2 2 6 3 6 12 4 6 15 5 6 18 
+8
source

Multiplication makes an essentially external product. A dot is an inner product if my memory with linear algebra is correct. Let me expand on the accepted answer:

 In [13]: df = pd.DataFrame({'A': [1., 1., 1., 2., 2., 2.], 'B': np.arange(1., 7.)}) In [14]: v1 = np.array([2,2,2,3,3,3]) In [15]: v2 = np.array([2,3]) In [16]: df.shape Out[16]: (6, 2) In [17]: v1.shape Out[17]: (6,) In [18]: v2.shape Out[18]: (2,) In [24]: df.mul(v2) Out[24]: AB 0 2 3 1 2 6 2 2 9 3 4 12 4 4 15 5 4 18 In [26]: df.dot(v2) Out[26]: 0 5 1 8 2 11 3 16 4 19 5 22 dtype: float64 

So:

df.mul takes the matrix of the form (6,2) and the vector (6, 1) and returns the matrix form (6,2)

While:

df.dot takes a matrix of the form (6,2) and a vector (2,1) and returns (6,1).

This is not the same operation, they are (I think) external and internal products, respectively.

+6
source

It is difficult to say with a certain degree of accuracy.

Often, a method exists and is undocumented because it is considered an internal provider and can be changed.

Of course, this can be simple oversight by the people who collected the documentation.

Regarding your second question; I do not know about this, but it would be better to ask a new S / O question. Just an API scan, can you do something with the DataFrame.applymap (function) function?

+1
source

All Articles