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

Divide Multiple Columns by Multiple other Columns

For two dataframes:

 df_1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) 

 df_2 = pd.DataFrame({'A': [3], 'B': [4]})

How do I divide all values in df_1 columns A and B by the respective values of A and B in df_2 so that:

 df_3 = pd.DataFrame({'A': [0.33, 0.66], 'B': [0.75, 1]})

I need to perform this operation over two large dataframes with the same headings. Please, scalable solutions.

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

>Solution :

You can use slicing of df_2 as Series:

df_1.div(df_2.iloc[0])

Or:

df_1.div(df_2.squeeze())

Output:

          A     B
0  0.333333  0.75
1  0.666667  1.00
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