Issue
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...
Solution
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'])
Answered By - Alexandra Dudkina
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.