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

Average of two pandas dataframes elements by elements results concatenated…why?

i have two dataframes:

print(d1.head())

         Codes       Prof Amp
477      0.7         3.0  0.724997
478      0.7         3.0  0.736914
479      0.7         3.0  0.612189
480      0.7         3.0  0.684321
481      0.7         3.0  0.950067

print(d1.shape)

(96, 3)

print(d2.head())

       Codes       Prof Amp
0      0.8         5.0  0.747135
1      0.8         5.0  1.370311
2      0.8         5.0  0.759630
3      0.8         5.0  1.125687
4      0.8         5.0  1.910926

print(d2.shape)

(96, 3)

when i use the following code:

dataM = d1.add(d2, fill_value=0)

        Code        Prof  Amp
0        0.8         5.0  0.747135
1        0.8         5.0  1.370311
2        0.8         5.0  0.759630
3        0.8         5.0  1.125687
4        0.8         5.0  1.910926

print(dataM.shape)

[192 rows x 3 columns]

but my target is

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

        Code        Prof  Amp
0        1.5         8.0  1.472132

……
and the shape should be the same [96 rows x 3 columns]

so how can i achieve that?

thank you in advance.

>Solution :

Reason is data alignment by index values – becuase different index values DataFrames are concatenated (and divided 2):

dataM = d1.add(d2, fill_value=0).div(2)
print (dataM)
     Codes  Prof       Amp
0     0.40   2.5  0.373567
1     0.40   2.5  0.685156
2     0.40   2.5  0.379815
3     0.40   2.5  0.562844
4     0.40   2.5  0.955463
477   0.35   1.5  0.362499
478   0.35   1.5  0.368457
479   0.35   1.5  0.306094
480   0.35   1.5  0.342160
481   0.35   1.5  0.475033

Need same index values in both DataFrames, so add DataFrame.reset_index with drop=True and for average divide by 2:

dataM = d1.reset_index(drop=True).add(d2.reset_index(drop=True), fill_value=0).div(2)
print (dataM)
   Codes  Prof       Amp
0   0.75   4.0  0.736066
1   0.75   4.0  1.053613
2   0.75   4.0  0.685909
3   0.75   4.0  0.905004
4   0.75   4.0  1.430496

Another idea is convert to numpy array and divide by 2:

dataM = d1.add(d2.to_numpy(), fill_value=0).div(2)
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