[Python] Combine Different Excel files with Python

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    I have a set of data in different excel files. For instance:

    Excel File #1:

    Date Data1 Data2
    1-1-2025 x1 x2
    1-2-2025 x3 x4


    Excel File #2:

    Date Data1 Data2
    1-3-2025 y1 y2
    1-4-2025 y3 y4


    I want to merge them into a single excel file:

    Excel File Merged

    Date Data1 Data2
    1-1-2025 x1 x2
    1-2-2025 x3 x4
    1-3-2025 y1 y2
    1-4-2025 y3 y4


    To do so, I am using the following code:

    for file_path in file_paths:
    # Get the date from the file name
    file_name = file_path
    file_name = os.path.splitext(file_name)[0]
    file_name = file_name[-8:]
    file_name = file_name.replace('_','/')
    # Read the Excel file
    df = pd.read_excel(file_path, sheet_name='Parte', usecols='O:AN', skiprows=10, nrows=110, header=[0])
    data_frames.append(df)
    combined_df = pd.concat(data_frames, ignore_index=True)
    combined_df.to_excel('rute1', index=False)


    As a result, the program do the merge, but the order of the data is not correct. As an example:

    Date Data1 Data2
    1-1-2025 x1 x2
    1-2-2025 x3 x4
    1-3-2025 y1 y2
    1-4-2025 y3 y4


    Can you help me with this issue?

    I want to make a succesfull merge as:

    Excel File Merged

    Date Data1 Data2
    1-1-2025 x1 x2
    1-2-2025 x3 x4
    1-3-2025 y1 y2
    1-4-2025 y3 y4

    Continue reading...

Compartilhe esta Página