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

How to make calculation between 2 dataframes by row and column position in an effective way?

I am trying to derive a new dataframe from 2 existing ones that calculates the percentage difference between the 2 existing dfs by row & column position. This is how the 2 dfs look like:

df1:
period_number   0      1        2   
0               1.0    0.55    0.35
1               1.0    0.40    0.20

df2:
period_number   0      1       2    
0               1.0    0.30    0
1               1.0    0.20    0

df_master (Note: df1 is the baseline for the percentage calculation against df2):
period_number   0           1                  2    
0               (1-1)/1    (0.55-0.3)/0.55     (0.35-0)/0.35
1               (1-1)/1    (0.40-0.2)/0.4      (0.20-0)/0.2

I tried merging but the format is just very hard to bring it to such calc, so not sure I need to combine pd.merge with something else?

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 :

df_master = (df1 - df2) / df1
df_master['period_number'] = df1.period_number
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