[Python] I'm not able to concat dataframes in python that have the same column name

Discussão em 'Python' iniciado por Stack, Julho 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new in Python and I'm facing an issue when I try to concat three dataframes.They all have the same column's names and the columns are in the same order. As final output I expect to have all the rows of df2 and df3 plugged in the end of df1.

    df1 looks like this:

    Employee_ID Country Gender
    1 USA Male
    2 Brazil Famale
    3 France Male

    df2:

    Employee_ID Country Gender
    4 Spain Male
    5 Mexico Famale
    6 England Famale

    df3:

    Employee_ID Country Gender
    7 USA Male
    8 Japan Male
    9 Spain Male

    When I tried to concat, my final output was:

    import pandas as pd
    df_final = pd.concat([df1, df2, df3])

    Employee_ID Country Gender Gender
    1 USA Male nan
    2 Brazil Famale nan
    3 France Male nan
    4 Spain Male nan
    5 Mexico Famale nan
    6 England Famale nan
    7 USA nan Male
    8 Japan nan Male
    9 Spain nan Male

    I was expecting something like this:

    Employee_ID Country Gender
    1 USA Male
    2 Brazil Famale
    3 France Male
    4 Spain Male
    5 Mexico Famale
    6 England Famale
    7 USA Male
    8 Japan Male
    9 Spain Male

    Note: I tried to check the data type in for column "Gender" in all the 3 dataframes and this is what I got as results:

    df3.Gender.dtype


    dtype('O')

    For df1 and df2 I got "AttributeError" :

    df1.Gender.dtype
    df2.Gender.dtype


    AttributeError: 'DataFrame' object has no attribute 'Gender'

    Any idea on how to solver this?

    Continue reading...

Compartilhe esta Página