append dataframe to excel with pandas

前端 未结 4 466
刺人心
刺人心 2020-11-27 07:13

I desire to append dataframe to excel

This code works nearly as desire. Though it does not append each time. I run it and it puts data-frame in excel. But each ti

4条回答
  •  庸人自扰
    2020-11-27 07:39

    I tried to read an excel, put it in a dataframe and then concat the dataframe from excel with the desired dataframe. It worked for me.

    def append_df_to_excel(df, excel_path):
        df_excel = pd.read_excel(excel_path)
        result = pd.concat([df_excel, df], ignore_index=True)
        result.to_excel(excel_path, index=False)
    
    df = pd.DataFrame({"a":[11,22,33], "b":[55,66,77]})
    append_df_to_excel(df, r"\.xlsx")
    

提交回复
热议问题