pandas Combine Excel Spreadsheets

后端 未结 2 1514
日久生厌
日久生厌 2020-12-10 15:44

I have an Excel workbook with many tabs. Each tab has the same set of headers as all others. I want to combine all of the data from each tab into one data frame (without rep

相关标签:
2条回答
  • 2020-12-10 16:18
    import pandas as pd  
    
    f = 'file.xlsx'
    df = pd.read_excel(f, sheet_name=None, ignore_index=True) 
    df2 = pd.concat(df, sort=True)
    
    df2.to_excel('merged.xlsx', 
                 engine='xlsxwriter', 
                 sheet_name=Merged,
                 header = True,
                 index=False)
    
    0 讨论(0)
  • 2020-12-10 16:35

    This is one way to do it -- load all sheets into a dictionary of dataframes and then concatenate all the values in the dictionary into one dataframe.

    import pandas as pd
    

    Set sheetname to None in order to load all sheets into a dict of dataframes and ignore index to avoid overlapping values later (see comment by @bunji)

    df = pd.read_excel('tmp.xlsx', sheet_name=None, index_col=None)
    

    Then concatenate all dataframes

    cdf = pd.concat(df.values())
    
    print(cdf)
    
    0 讨论(0)
提交回复
热议问题