Home > Mobile >  Row by row multiplication on pandas or numpy
Row by row multiplication on pandas or numpy

Time:02-02

I have one dataframe with classes and two components and a second one with elements and the same components.

df1:

df1 = pd.DataFrame({'Class':['banana', 'apple'], 'comp1':[1, 2], 'comp2':[-5, 4]})

df2:

df2 = pd.DataFrame({'Element':['K', 'Mg'], 'comp1':[3, -4], 'comp2':[1, 3]})

I want to multiply them row by row in a way that would generate the following output:

output = pd.DataFrame({'Class': ['banana', 'banana', 'apple', 'apple'], 'Element': ['K', 'Mg', 'K', 'Mg'], 'comp1':[3, -4, 6, -8], 'comp2':[-5, -15, 4, 12]})

Could you help me?

CodePudding user response:

well as i see it's like cartesian product. and then some manipulation for desired output as mentioned.

import pandas as pd

df1 = pd.DataFrame({'Class':['banana', 'apple'], 'comp1':[1, 2], 'comp2':[-5, 4]})
df2 = pd.DataFrame({'Element':['K', 'Mg'], 'comp1':[3, -4], 'comp2':[1, 3]}) 
#merging data
output  = df1.merge(df2, how='cross')

output['comp1'] = output.pop('comp1_x') * output.pop('comp1_y')
output['comp2'] = output.pop('comp2_x') * output.pop('comp2_y')
print(output)

expected = pd.DataFrame({'Class': ['banana', 'banana', 'apple', 'apple'], 'Element': ['K', 'Mg', 'K', 'Mg'], 'comp1':[3, -4, 6, -8], 'comp2':[-5, -15, 4, 12]})
print(expected.equals(output)) # True
'''
    Class Element  comp1  comp2
0  banana       K      3     -5
1  banana      Mg     -4    -15
2   apple       K      6      4
3   apple      Mg     -8     12


'''
  • Related