Home > front end >  np.multiply or pd.mul but what multiplying array to use depends on conditions
np.multiply or pd.mul but what multiplying array to use depends on conditions

Time:10-17

I have two DataFrames df1 and df2 and two arrays mul1 and mul2

import numpy as np
import pandas as pd

df1 = pd.DataFrame({'A': [100, 200], 'B': [50, 150], 'C': [40, 120]})
#      A    B    C
# 0  100   50   40
# 1  200  150  120
df2 = pd.DataFrame({'A': [5, 1], 'B': [3, 25], 'C': [10, 11]})
#    A   B   C
# 0  5   3  10
# 1  1  25  11

mul1 = np.array([.1, .2, .3])
mul2 = np.array([1.1, 1.2, 1.3])

I would like to use something like pd.mul or np.multiply to multiply df1 by elements in either mul1 or mul2 depending on their corresponding values in df2.

For column A, df1 needs to be multiplied by the mul1[0] if df2['A'] > 3, otherwise it needs to be multiplied by mul2[0].

For columns B and C, the corresponding criteria is df2['B'] > 12 and df2['C'] > 15.

So the correct output is:

    A    B   C
0   10   60  52
1  220   30  156

Does anyone have a good way to do this?

CodePudding user response:

Using np.where, we can specify a condition in the 1st parameter, and the operations we want in the 2nd and 3rd parameters:

df1['A'] = np.where(df2['A'] > 3, df1['A'] * mul1[0], df1['A'] * mul2[0])
df1['B'] = np.where(df2['B'] > 12, df1['B'] * mul1[1], df1['B'] * mul2[1])
df1['C'] = np.where(df2['C'] > 15, df1['C'] * mul1[2], df1['C'] * mul2[2])
  • Related