Multiply all columns in a Pandas dataframe together
By : user6177004
Date : March 29 2020, 07:55 AM
seems to work fine You could use DataFrame.prod(): code :
>>> df = pd.DataFrame(np.random.randint(1, 10, (5, 3)))
>>> df
0 1 2
0 7 7 5
1 1 8 6
2 4 8 4
3 2 9 5
4 3 8 7
>>> df.prod(axis=1)
0 245
1 48
2 128
3 90
4 168
dtype: int64
>>> df = pd.DataFrame(np.random.randint(1, 10, (5, 3)))
>>> df
0 1 2
0 9 3 3
1 8 5 4
2 3 6 7
3 9 8 5
4 7 1 2
>>> df.apply(np.prod, axis=1)
0 81
1 160
2 126
3 360
4 14
dtype: int64

How to selectively multiply or add columns in pandas DataFrame?
By : Les Hankel
Date : March 29 2020, 07:55 AM
hop of those help? Just to throw out an alternative (you can use the prod DataFrame method): code :
In [11]: df['multiplied'] = df[df != 0].prod(axis=1)
In [12]: df
Out[12]:
P1 P2 P3 multiplied
Sample
A 0.25 0.50 0.25 0.03125
B 0.25 0.00 0.25 0.06250
C 0.25 0.25 0.00 0.06250

Multiply list of columns in pandas DataFrame
By : Rou Mass
Date : March 29 2020, 07:55 AM
hop of those help? Select the columns in the list using loc and then use .prod() across the rows (by specifying axis=1). For example: code :
>>> cols = ['A','B','C','D']
>>> df = pd.DataFrame(np.random.randint(1, 4, size=(3,4)), columns=list('ABCD'))
>>> df
A B C D
0 2 2 1 1
1 2 3 1 1
2 3 1 3 3
>>> df['VAL'] = df.loc[:, cols].prod(axis=1)
>>> df
A B C D VAL
0 2 2 1 1 4
1 2 3 1 1 6
2 3 1 3 3 27

Multiply two pandas dataframe with similar columns
By : LeonD
Date : March 29 2020, 07:55 AM
Hope that helps It looks like you want to perform a dot product on the values. This should be pretty fast  code :
r = df_1.values.dot(df_2_t.values.T)

How can I multiply each column with the other columns of the Pandas DataFrame?
By : Darren Morris
Date : March 29 2020, 07:55 AM
should help you out The following is a brute force method, but it should do the job. permutations() generates all the column permutations. The inner sorted() together with set() merges ('A','B') with ('B','A'), etc.

