Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Row by row multiplication on pandas or numpy

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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


'''
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading