-1

I am trying to append 10 Excel files to one in Python,

The code below was used and I am getting

TypeError: first argument must be an iterable of pandas objects, 
you passed an object of type "DataFrame"

Once I change sheet_name argument to None, the code run perfectly. However, all the 10 excel files has three sheets and I only want specific sheet per excel file. Is there a way to get it done?

your help is appreciated.

  import pandas as pd
    import glob
    path = r'Folder path'
    filenames = glob.glob(path + "\*.xlsx")
    finalexcelsheet = pd.DataFrame()
    for file in filenames:
        df = pd.concat(pd.read_excel(file, sheet_name= 'Selected Sheet'), ignore_index=True,sort=False)
        finalexcelsheet=finalexcelsheet.append(df,ignore_index=True) 
furas
  • 134,197
  • 12
  • 106
  • 148
J.p
  • 43
  • 6
  • does this answer your question? https://stackoverflow.com/questions/47743741/combining-excel-workbook-sheet-into-one-using-python?rq=1 – Thulfiqar Jun 13 '21 at 19:03
  • aalways put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot, not link to external portal). There are other useful information. – furas Jun 13 '21 at 23:35
  • 1
    I think you use `pd.concat` in wrong way - and maybe you don't even need it. `pd.concat` nead list with many `dataframes` to concatenate them - if you use `sheet_name=None` then `read_excel` may gives you list with `dataframes` (every sheet in separated `dataframe`) but if you use name then you have only one `dataframe` and it has nothing to `concatenate`. And if you have only one `dataframe` then you don't need `concatenate` or you should do `finalexcelsheet = pd.concat( [finalexcelsheet, pd.read_excel(...)], ...)`. – furas Jun 13 '21 at 23:42
  • FIrst you could assing `read_excel()` to variable and check what you get when you use `sheet_name=None` and `sheet_name= 'Selected Sheet'` – furas Jun 13 '21 at 23:42

1 Answers1

0

I can't test it but problem is because you use concat in wrong way - or rather because you don't need concat in your situation.


concat needs list with dataframes like

concat( [df1, df2, ...], ...)

but read_excel gives different objects for different sheet_name=... and this makes problem.

read_excel for sheet_name=None returns list or dict with all sheets in separated dataFrames

[df_sheet_1, df_sheet_2, ...]

and then concat can join them to one dataframe

read_excel for sheet_name=name returns single dataframe

df_sheet

and then concat has nothing co join - and it gives error.
But it means you don't need concat.

You should directly assign read_excel to df

for file in filenames:
    df = pd.read_excel(file, sheet_name='Selected Sheet')
    finalexcelsheet = finalexcelsheet.append(df, ignore_index=True) 
furas
  • 134,197
  • 12
  • 106
  • 148