Home > OS >  How to iterate in an specific sheet using [DataFrame].concat?
How to iterate in an specific sheet using [DataFrame].concat?

Time:08-11

I have a problem here, N files with 4 sheets, but i want just a specific sheet of each file that´s named "STD " and it´s the first sheet in every file.

Could u help me?

Follow the code...

# reading all the excel files
filenames = glob.glob(path   "\*.xlsx")
print('File names:', filenames)
  
# initializing empty data frame
finalexcelsheet = pd.DataFrame()
  
# to iterate excel file one by one 
# inside the folder
for file in filenames:
  
    # combining multiple excel worksheets
    # into single data frames
    df = pd.concat(pd.read_excel(file, sheet_name= 0), ignore_index=True, sort= True)
  
    # appending excel files one by one
    finalexcelsheet = finalexcelsheet.append(
      df, ignore_index=True)

At the line: df = pd.concat(pd.read_excel(file, sheet_name= 0), ignore_index=True, sort= True)

It´s returning the error:

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

df = pd.concat(pd.read_excel(file, sheet_name= 0), ignore_index=True, sort= True)

CodePudding user response:

concat() needs a sequence of DataFrames as a parameter, to force that you can use [] over the DataFrame object from read_excel() function as follows :

df_std  = pd.read_excel(file, sheet_name= 0)
df = pd.concat([df_std], ignore_index=True, sort= True)

Note that you can add many DataFrames as well :

df = pd.concat([df_std1,df_std2,df_std3...], ignore_index=True, sort= True)

Hope that your problem will be solved by this

  • Related