Appending Pandas DataFrame to existing Excel document

前端 未结 3 1999
梦如初夏
梦如初夏 2021-01-13 11:37

Per https://github.com/pandas-dev/pandas/pull/21251/files/09e5b456e1af5cde55f18f903ab90c761643b05a, we should be able to append DataFrames to new XLSX sheets.

Based

3条回答
  •  离开以前
    2021-01-13 11:54

    import pandas as pd

    writer = pd.ExcelWriter(wk_path + save_file)
    # ....
    # build sc_files DataFrame and save. sc_files includes
    # a column called OS.
    
    sc_file.to_excel(writer, sheet_name='test')
    
    # build data frame of OS counts out of sc_file
    counts_os = sc_file.OS.value_counts() 
    
    # To append to 'test' sheet, use startcol=x1, startrow=y
    # To append counts_OS to the end of the current 'test' sheet
    y = len(sc_file)
    y += 1
    counts_os.to_excel(writer, sheet_name='test', 
        startcol=1, startrow=y)
    
    # write counts_os to sheet test2 
    counts_os.to_excel(writer, sheet_name='test2')
    writer.save()
    writer.close()
    

提交回复
热议问题