代码之家  ›  专栏  ›  技术社区  ›  Chris

在多个循环上组合Excel工作表

  •  1
  • Chris  · 技术社区  · 6 年前

    类型错误:第一个参数必须是pandas对象的iteable,您

    import pandas as pd    
    
    d = 2013
    numberOfSheets = 5
    
    while d < 2015:
        #print(str(d) + ' beginning')
        f ='H:/MyDocuments/Z Project Work/scriptTest ' + str(d) + '.xlsx'  
        for i in range(1,numberOfSheets+1):
            data = pd.read_excel(f, sheetname = 'Table '+str(i), header=None) 
            print(i)
            df.append(data)   
    
        print(str(d) + ' complete')
        print(df)
        d += 1
    
    df = pd.concat(df)
    print(df)
    
    final = "H:/MyDocuments/Z Project Work/mergedfile.xlsx" 
    df.to_excel(final)
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   Peter Leimbigler    6 年前

    pd.concat() 需要ITerable,如列表: pd.concat([df1, df2]) df1 df2 沿着默认的0轴,这意味着

    有两个问题需要解决:

    1. for df
    2. 测向 对于

    import pandas as pd    
    
    d = 2013
    numberOfSheets = 5
    dfs = []
    
    while d < 2015:
        #print(str(d) + ' beginning')
        f ='H:/MyDocuments/Z Project Work/scriptTest ' + str(d) + '.xlsx'  
        for i in range(1, numberOfSheets + 1):
            data = pd.read_excel(f, sheetname='Table ' + str(i), header=None) 
            print(i)
            dfs.append(data)
    
        print(str(d) + ' complete')
        print(df)
        d += 1
    
    # ignore_index=True gives the result a default IntegerIndex 
    # starting from 0
    df_final = pd.concat(dfs, ignore_index=True)
    print(df_final)
    
    final_path = "H:/MyDocuments/Z Project Work/mergedfile.xlsx" 
    df_final.to_excel(final_path)
    
        2
  •  2
  •   123 Padraic Cunningham    6 年前

    :设置 sheet_name=None

    dfs = {<sheetname1>: <DataFrame1>, <sheetname2>: <DataFrame2>, etc.}  
    

    xl = pd.ExcelFile(fpath)
    dfs = xl.parse(sheetname=None, header=None)
    
    for i, df in enumerate(dfs):
        <do stuff with each, if you want>
        print('Sheet {0} looks like:\n{1}'.format(i+1, df))
    
        3
  •  1
  •   Chris    6 年前

    import pandas as pd
    import glob
    #import numpy as np
    #import os, collections, csv
    #from os.path import basename    
    
    fpath = "H:/MyDocuments/Z Project Work/"
    
    dfs = []
    files = glob.glob(fpath+'*.xlsx') 
    
    for f in files:
        xl = pd.ExcelFile(f) 
        xls = xl.parse(sheetname=None, header=0)
        for i, df in enumerate(xls):
            print(i)    
            dfs.append(xls[df])   
    
        print(f+ ' complete')
    
    df_final = pd.concat(dfs, ignore_index=True)
    
    final = "H:/MyDocuments/Z Project Work/mergedfile.xlsx" 
    df_final.to_excel(final)