Append existing excel sheet with new dataframe using python pandas

Posted on

Question :

Append existing excel sheet with new dataframe using python pandas

I currently have this code. It works perfectly.

It loops through excel files in a folder,
removes the first 2 rows, then saves them as individual excel files,
and it also saves the files in the loop as an appended file.

Currently the appended file overwrites the existing file each time I run the code.

I need to append the new data to the bottom of the already existing excel sheet (‘master_data.xlsx)

dfList = []
path = 'C:\Test\TestRawFile' 
newpath = 'C:\Path\To\New\Folder'

for fn in os.listdir(path): 
  # Absolute file path
  file = os.path.join(path, fn)
  if os.path.isfile(file): 
    # Import the excel file and call it xlsx_file 
    xlsx_file = pd.ExcelFile(file) 
    # View the excel files sheet names 
    # Load the xlsx files Data sheet as a dataframe 
    df = xlsx_file.parse('Sheet1',header= None) 
    df_NoHeader = df[2:] 
    data = df_NoHeader 
    # Save individual dataframe
    data.to_excel(os.path.join(newpath, fn))


appended_data = pd.concat(dfList)
appended_data.to_excel(os.path.join(newpath, 'master_data.xlsx'))

I thought this would be a simple task, but I guess not.
I think I need to bring in the master_data.xlsx file as a dataframe, then match the index up with the new appended data, and save it back out. Or maybe there is an easier way. Any Help is appreciated.

Asked By: brandog


Answer #1:

A helper function for appending DataFrame to existing Excel file:

def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None,
    Append a DataFrame [df] to existing Excel file [filename]
    into [sheet_name] Sheet.
    If [filename] doesn't exist, then this function will create it.

      filename : File path or existing ExcelWriter
                 (Example: '/path/to/file.xlsx')
      df : dataframe to save to workbook
      sheet_name : Name of sheet which will contain DataFrame.
                   (default: 'Sheet1')
      startrow : upper left cell row to dump data frame.
                 Per default (startrow=None) calculate the last row
                 in the existing DF and write to the next row...
      truncate_sheet : truncate (remove and recreate) [sheet_name]
                       before writing DataFrame to Excel file
      to_excel_kwargs : arguments which will be passed to `DataFrame.to_excel()`
                        [can be dictionary]

    Returns: None
    from openpyxl import load_workbook

    import pandas as pd

    # ignore [engine] parameter if it was passed
    if 'engine' in to_excel_kwargs:

    writer = pd.ExcelWriter(filename, engine='openpyxl')

    # Python 2.x: define [FileNotFoundError] exception if it doesn't exist 
    except NameError:
        FileNotFoundError = IOError

        # try to open an existing workbook = load_workbook(filename)

        # get the last row in the existing Excel sheet
        # if it was not specified explicitly
        if startrow is None and sheet_name in
            startrow =[sheet_name].max_row

        # truncate sheet
        if truncate_sheet and sheet_name in
            # index of [sheet_name] sheet
            idx =
            # remove [sheet_name]
            # create an empty sheet [sheet_name] using old index
  , idx)

        # copy existing sheets
        writer.sheets = {ws.title:ws for ws in}
    except FileNotFoundError:
        # file does not exist yet, we will create it

    if startrow is None:
        startrow = 0

    # write out the new sheet
    df.to_excel(writer, sheet_name, startrow=startrow, **to_excel_kwargs)

    # save the workbook

Usage examples…

Old answer: it allows you to write a several DataFrames to a new Excel file.

You can use openpyxl engine in conjunction with startrow parameter:

In [48]: writer = pd.ExcelWriter('c:/temp/test.xlsx', engine='openpyxl')

In [49]: df.to_excel(writer, index=False)

In [50]: df.to_excel(writer, startrow=len(df)+2, index=False)

In [51]:


enter image description here

PS you may also want to specify header=None if you don’t want to duplicate column names…

UPDATE: you may also want to check this solution

Answered By: MaxU

Answer #2:

If you aren’t strictly looking for an excel file, then get the output as csv file and just copy the csv to a new excel file

df.to_csv('filepath', mode='a', index = False, header=None)

mode = ‘a’

a means append

This is a roundabout way but works neat!

Answered By: David

Answer #3:

This question has been out here a while. The answer is ok, but I believe this will solve most peoples question.

simply use glob to access the files in a specific directory, loop through them, create a dataframe of each file, append it to the last one, then export to a folder. I also included commented out code to run through this with csvs.

import os
import pandas as pd
import glob

# put in path to folder with files you want to append
# *.xlsx or *.csv will get all files of that type
path = "C:/Users/Name/Folder/*.xlsx"
#path = "C:/Users/Name/Folder/*.csv"

# initialize a empty df
appended_data = pd.DataFrame()

#loop through each file in the path
for file in glob.glob(path):

    # create a df of that file path
    df = pd.read_excel(file, sheet_name = 0)
    #df = pd.read_csv(file, sep=',')

    # appened it
    appended_data = appended_data.append(df)


# export the appeneded data to a folder of your choice
exportPath = 'C:/My/EXPORT/PATH/appended_dataExport.csv'
Answered By: brandog

Answer #4:

Complementing to @david, if you dont care the index and you can use .csv, this function helps to append any df to an existing csv

def append_df(self, path_file, df):
    with open(path_file, 'a+') as f:
        df.to_csv(f, header=f.tell() == 0, encoding='utf-8', index=False)


a+ create the file if it doesnot exist

f.tell() == 0 add header if the first row

Answered By: Alex Montoya

Leave a Reply

Your email address will not be published.