-1

I am working with two data frames that I joined using

 df = pd.concat([hc,nh], ignore_index=True, sort=False)

and now need to combine a few like-columns. For example:

Country   Country_ID  
1         
2         
             3
             4

How do I make it so it'd look like this:

Country
1
2
3
4        

I saw this post that is a similar question to mine, but the answer there didn't work for me...

mymojoh
  • 3
  • 2
  • 1
    Do not include your code/data as images. Copy/paste the text in the question. – Quang Hoang Oct 26 '20 at 18:31
  • you can remane the column *as* you merge in the dataframe. – David Erickson Oct 26 '20 at 18:32
  • Are `"Country"` and `"Country ID"` identical? Why not just drop one of them? – JAV Oct 26 '20 at 18:43
  • @DavidErickson May you please show me an example of how I can do that? – mymojoh Oct 26 '20 at 18:44
  • @JAV They are not identical, so it would be like x has rows 1-5 and y has rows 6-10. And the column xy (or x?) that I want both to merge to would have rows 1-10. Sorry, this is my first time posting a question so I'm not familiar with the syntax to show code blocks properly... – mymojoh Oct 26 '20 at 18:47
  • @mymojoh please see https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples . The answers in there should help you understand how you can improve your question so that can someone can answer. – David Erickson Oct 26 '20 at 19:08

1 Answers1

0

Assuming you have a dataframe:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    'Country': [1,2, None, None],
    'Country ID': [None, None, 3, 4]
})

Data from one column can be assigned to another one using np.where():

df['Country'] = np.where(df['Country'].isna(), df['Country ID'], df['Country'])
Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27