Excel file overwritten instead of concat - Python - Pandas

后端 未结 3 1156
情话喂你
情话喂你 2021-01-16 05:44

I\'m trying to contact all excel files and worksheets in them into one using the below script. It kinda works but then the excel file c.xlsx is overwritten per file, so only

3条回答
  •  不知归路
    2021-01-16 05:50

    I got it working using the below script which uses @ryguy72's answer but works on all worksheets as well as the header row.

    import pandas as pd
    import numpy as np
    import glob
    
    all_data = pd.DataFrame()
    for f in glob.glob("my_path/*.xlsx"):
        df = pd.read_excel(f, sheet_name=None, ignore_index=True)
        cdf = pd.concat(df.values())
        all_data = all_data.append(cdf,ignore_index=True)
    print(all_data)
    df = pd.DataFrame(all_data)
    df.shape
    df.to_excel("my_path/final.xlsx", sheet_name='Sheet1')
    

提交回复
热议问题