简体   繁体   English

我想将两个具有相同名称列的数据框组合到熊猫中

[英]I want to combine two dataframe with the same names columns in pandas

I would like to combine these Dataframe: 我想结合这些数据框:

df_a = pd.DataFrame(data={
    'Nombre': [Elisa Perez],
    'Fecha': [2/04/2019],
    'Sexo': [np.nan],
    'Nacionalidad': [np.nan],
    'Ciudad': [Roma]})

df_b = pd.DataFrame(data={
    'Nombre': [Elisa Perez],
    'Fecha': [2/04/2019],
    'Sexo': [mujer],
    'Nacionalidad': [Italiana],
    'Ciudad': [Roma]})

and I want automatically this result without have to write each columns names in the code: 而且我希望自动获得此结果,而不必在代码中写入每个列的名称:

    Nombre       Fecha       Sexo       Nacionalidad       Ciudad
0 Elisa Perez  2/04/2019     mujer       Italiana           Roma 

Thanks!! 谢谢!!

Use DataFrame.combine_first with DataFrame.set_index for columns for matching: 使用DataFrame.combine_firstDataFrame.set_index进行匹配的列:

df = (df_b.set_index(['Nombre','Fecha'])
          .combine_first(df_a.set_index(['Nombre','Fecha']))
          .reset_index())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM