Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 837
Amelio Vazquez-Reina Avatar asked Apr 02 '13 00:04

Amelio Vazquez-Reina


People also ask

What is ILOC return?

iloc returns a Pandas Series when one row is selected, and a Pandas DataFrame when multiple rows are selected, or if any column in full is selected. To counter this, pass a single-valued list if you require DataFrame output.

Does pandas use PyArrow?

To interface with pandas, PyArrow provides various conversion routines to consume pandas structures and convert back to them.

How do you get a value from a cell of a Pandas DataFrame?

Method 1 : Get a value from a cell of a Dataframe using loc() function. Pandas DataFrame. loc attribute access a group of rows and columns by label(s) or a boolean array in the given DataFrame. Here, we will use loc() function to get cell value.

What is the use of .values in pandas?

The values property is used to get a Numpy representation of the DataFrame. Only the values in the DataFrame will be returned, the axes labels will be removed. The values of the DataFrame. A DataFrame where all columns are the same type (e.g., int64) results in an array of the same type.


1 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]: 
   A   B
0  2   2
1  2   4
2  2   6
3  6  12
4  6  15
5  6  18
like image 57
unutbu Avatar answered Sep 21 '22 15:09

unutbu