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

Add column values in a dataframe to values in another dataframe with different length

There is a dataframe df:

     test_num  el_num  file_num  value   fail
429         4      1         0   3.36  False
430         4      1         1   3.29  False
431         4      1         2   3.29  False
432         4      1         3   3.26  False
433         4      1         4   3.28  False
434         4      1         5   3.26  False
435         4      1         6   3.27  False
436         4      1         7   3.26  False
437         4      1         8   3.26  False
438         4      1         9   3.18  False
439         4      1        10   3.24  False
440         4      1        11   3.27  False
441         4      1        12   3.32  False
429         5      2         0   3.36  False
430         5      2         1   3.29  False
431         5      2         2   3.29  False
432         5      2         3   3.26  False
433         5      2         4   3.28  False
434         5      2         5   3.26  False
435         5      2         6   3.27  False
436         5      2         7   3.26  False
437         5      2         8   3.26  False
438         5      2         9   3.18  False
439         5      2        10   3.24  False
440         5      2        11   3.27  False
441         5      2        12   3.32  False

and a dataframe diff:

     value  el_num  test_num
198   0.43       1         5
204   0.43       2         5
210   0.46       3         5
216   0.42       4         5
222   0.41       5         5
228   0.43       6         5
234   0.46       7         5
240   0.43       8         5
246   0.44       9         5
252   0.41      10         5
258   0.19      11         5

How to add diff.value to df.value and put it to df.value with corresponding test_num and el_num?

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 :

Merge then add:

df = (df.merge(diff, on=['el_num', 'test_num'], how='left')
   .assign(value=lambda x: x.pop('value_x') + x.pop('value_y').fillna(0))
)
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