如何在熊猫上加入2列?

时间:2020-10-26 18:30:50

标签: python pandas dataframe jupyter-notebook

我正在处理我使用的两个数据框

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

,现在需要合并一些类似的列。例如:

Country   Country_ID  
1         
2         
             3
             4

我如何制作它,使其看起来像这样:

Country
1
2
3
4        

我看到了this post,这与我的问题类似,但是那里的答案对我没有用...

1 个答案:

答案 0 :(得分:0)

假设您有一个数据框:

import pandas as pd
import numpy as np

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

可以使用np.where()将一列中的数据分配给另一列:

df['Country'] = np.where(df['Country'].isna(), df['Country ID'], df['Country'])