Multiply vector on dataframe - vectorized

pandas multiply
pandas multiply columns by other column
python multiply column by number
pandas multiply two columns element wise
pandas matrix multiplication
pandas sumproduct
pandas multiply all columns
pandas multiply column by float

I have a pandas dataframe of size 2441x1441 (A), which is zero in the upper triangle - the diagnoal has values. I would like to multiply each column of this with a vector of length 2441 (B). The tricky part is, that I want the first non-zero value of A multiplied with the first value of B (and second value of A with second value of B and so on). This should happen for all columns of A and result in another dataframe, C.

[[1, 0, 0],
[3, 4, 0],
[6, 7, 8]])


Here the result would be

C=[ 1,  0, 0,
    6,  4, 0,
   18, 14, 8]

I have made a for loop, where I can iterate through each value

for x in range(0,len(B)):
    C = (A.iloc[192+x:,:].T*B[0:len(B)-x]).T

However this is very slow, and I need to repeat this operation many times on different datasets. Is there a nice and pythonic way of vectorizing this?

Use np.fromfunction to define a matrix whose entries are the multipliers that you want. For example, if

A = np.array([[1, 0, 0],
              [3, 4, 0],
              [6, 7, 8]])


B = np.clip(np.fromfunction(lambda i, j: i-j+1, A.shape), 0, None)

will give you

B = np.array([[1, 0, 0],
              [2, 1, 0],
              [3, 2, 1]])

and then the result you want is simply the elementwise product of A and B:

C = A * B


C = np.array([[1,  0,  0],
              [6,  4,  0],
              [18, 14, 8]])

In fact, since your A is lower-triangular, you can drop the call to np.clip in the definition of B and obtain the same C.

Edit: I slightly misinterpreted the question. If the B in the OP (let me call it b, since I've already used B) is not the sequence of natural numbers, you can do

 B = np.tril(
             lambda i, j: b[np.clip((i-j).astype(int), 0, b.shape[0])],

For example, if

 b = np.array([2, 3, 1, 4])

then you would get

 B = np.array([[2, 0, 0],
               [3, 2, 0],
               [1, 3, 2]])

pandas.DataFrame.multiply, Get Multiplication of dataframe and other, element-wise (binary operator mul ). Equivalent to dataframe * other , but with support to substitute a fill_value for  Returns Vector. The result of multiplying scalar and vector.. Examples. The following example shows how to use this method to multiply a scalar by a Vector.. private Vector multiplyVectorByScalarExample2() { Vector vector1 = new Vector(20, 30); Double scalar1 = 75; Vector vectorResult = new Vector(); // Multiply the vector by the scalar.

OK, so how about creating an array from your B vector that matches the shape you're after? Once you've transformed it in that way, you could perform an element-wise multiplication, and all the right values will be aligned.

A = np.array([[1, 0, 0],
              [3, 4, 0],
              [6, 7, 8]])
B = np.array([1,2,3,4])

mB = B[:A.shape[0]]
shift = B[:A.shape[0]]
for b in range(0,A.shape[0]):
    shift = np.roll(shift ,1)
    mB = np.append(mB, shift)

>>>> array([[1, 0, 0],
            [2, 1, 0],
            [3, 2, 1]])

In the above I force the top-right triangle to be zeros, but since your A vector has already got zeros in those positions, it doesn't really matter what values are going to be in those positions of the multiplying array - so the np.tril step is not really necessary.

Anyway, whatever your preference, once you've got that form (and there may well be a better way than the one used above to arrive at that form) then you can np.multiply the two objects which will multiply aligned elements.

np.multiply(A, np.tril(mB.T))

>>>> array([[ 1,  0,  0],
            [ 6,  4,  0],
            [18, 14,  8]]), Compute the matrix multiplication between the DataFrame and other. This method computes the matrix product between the DataFrame and the values of an other  Dump the loops: Vectorization with NumPy. Many calculations require to repeatedly do the same operations with all items in one or several sequences, e.g. multiplying two vectors a = [1, 2, 3, 4, 5] and b = [6, 7, 8, 9, 10].

Here's a way to do it:

You can create a lower triangular matrix from B, by trimming and zero padding the vector B over each column so that its upper triangular part are all zeros.

So in essence this way you are replicating a matrix multiplication operation. You then simply have to multiply element-wise the two matrices by either using A*new_B or np.multiply(A,new_B).

new_b = np.array([list(np.pad(B[:-i] if i != 0 else B,(i,0), 'constant')) 
                  for i in range(len(B))]).T[:len(A),:len(A)]

array([[1, 0, 0],
       [2, 1, 0],
       [3, 2, 1]])

array([[ 1,  0,  0],
       [ 6,  4,  0],
       [18, 14,  8]])

Vectorization and parallelization in Python with NumPy and Pandas , Many calculations require to repeatedly do the same operations with all items in one or several sequences, e.g. multiplying two vectors a = [1,  DataFrame.multiply (self, other, axis = 'columns', level = None, fill_value = None) [source] ¶ Get Multiplication of dataframe and other, element-wise (binary operator mul ). Equivalent to dataframe * other , but with support to substitute a fill_value for missing data in one of the inputs.

How to multiply two pandas DataFrame columns in Python, operator(*). The mul() method provides a parameter fill_value using which values can be passed to replace the np. How to Multiply Vectors by a Scalar. When you multiply a vector by a scalar, each component of the vector gets multiplied by the scalar. Suppose we have a vector , that is to be multiplied by the scalar . Then, the product between the vector and the scalar is written as . If , then the multiplication would increase the length of by a factor .

Multiplying a DataFrame with another DataFrame, Series or a , Matrix multiplication operates by taking dot products of the rows of the first array (​matrix) with the columns of the second. Let's say I have a matrix X, and →Xi,: is  If two vectors are of unequal length, the shorter one will be recycled in order to match the longer vector. For example, the following vectors u and v have different lengths, and their sum is computed by recycling values of the shorter vector u .

Vector and matrix dot products, “np.outer”, Vectorization with Pandas series 5. Vectorization with NumPy arrays In other words, a DataFrame is a matrix of rows and columns that have  Consequently, you should see a significant runtime improvement when using the multi-dimensional matrix multiply operations contained in this code. mntimes will support 2-D matrix multiplication much like the built-in mtimes.m routine (so you can overload it if desired), it will support 3-D matrix multiplication, 4-D multiplication, and so on