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

why doesn't pandas column get overwritten by other column?

I am trying to overwrite the row values for column A and B in df1 with the values from df2. My dfs look as such:

df1
    'A'  'B'   'C'
23   0   cat   orange
24   0   cat   orange
25   0   cat   orange

df2
    'A'  'B'   'C'
56   2   dog   yellow
64   4   rat   orange
85   2   bat   red

The indices here are different and I would like to overwrite row 25 of df1 with the values of 64 from df2 for only column A and B.

I have tried something like this

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

df1[['A','B']].loc[25] = df2[['A','B']].loc[64]

This executes but doesn’t actually seem to overwrite anything as when I call df1[['A','B']].loc[25] I still get the original values. I would expect the new df1 to look like this:

df
    'A'  'B'   'C'
23   0   cat   orange
24   0   cat   orange
25   2   bat   orange

Can someone explain why this doesn’t work for me please?

>Solution :

df1.loc[25, ['A', 'B']] = df2.loc[64, ['A', 'B']]
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