Search code examples
pythonpandasinsertanalysis

Insert data at end of worksheet with pandas in python


I'm working with a data analysis and I have a question that I can't solve, I have 2 .xlsx sheets, both are the same, only with data in different columns, I wanted to add the data from sheet 2, at the end of sheet 1

First I imported the two files

file_excel  = pd.read_excel("feedback.xlsx", engine='openpyxl')
file_insert  = pd.read_excel("insert_fed.xlsx", engine='openpyxl')

file_excel  

and returned me

Number  Color
0   White
14  Black
0   White
13  Black
6   Red
7   Red
4   Red
12  Black
9   Black
6   Red
12  Black

and then I ran the second file

file_insert

Número  Cor
13  Black
9   Black
14  Black
2   Red
9   Black
9   Black
3   Red
13  Black
8   Black
11  Black
13  Black

And I wanted the code to 'add' these two files and it would return me this

Number  Color
0   White
14  Black
0   White
13  Black
6   Red
7   Red
4   Red
12  Black
9   Black
6   Red
12  Black
13  Black
9   Black
14  Black
2   Red
9   Black
9   Black
3   Red
13  Black
8   Black
11  Black
13  Black

Is there any way to do it using pandas?


Solution

  • It seems that your excelfiles/sheets have different headers, so make sure first that the two dataframes have the same columns (by using pandas.DataFrame.columns) then use pandas.concat.

    Try this :

    file_insert.columns= file_excel.columns
    
    out_df= pd.concat([file_excel, file_insert], ignore_index=True)
    

    # Output :

    print(out_df)
    
        Number  Color
    0        0  White
    1       14  Black
    2        0  White
    3       13  Black
    4        6    Red
    5        7    Red
    6        4    Red
    7       12  Black
    8        9  Black
    9        6    Red
    10      12  Black
    11      13  Black
    12       9  Black
    13      14  Black
    14       2    Red
    15       9  Black
    16       9  Black
    17       3    Red
    18      13  Black
    19       8  Black
    20      11  Black
    21      13  Black