0

I'm currently creating a dataframe from an excel spreadsheet in Pandas. For most of the files, they only contain 1 sheet. However, with some of the files that I have the sheet is not the first sheet. However, all of the sheets in all of the files have the same format. They have 'ITD_XXX_XXXX'. Is there a way to input into pandas to select the sheet that has the form.

df = pd.read_excel(path, sheet_name = contains('ITD_')

Here pandas would only select data from the sheet that has the string 'ITD_' in front of it?

Cheers.

Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32
Nhyi
  • 373
  • 1
  • 12
  • 1
    does this help? https://stackoverflow.com/a/17977609/10291291 – Quixotic22 Oct 08 '21 at 14:41
  • So the way I identify it is if the sheet_name contains the specific string 'ITD_' as the other X's can change and it's not possible to keep track. – Nhyi Oct 08 '21 at 14:48

1 Answers1

1

I think the answer here would probably give you what you need.

Bring in the file as an Excelfile before reading it as a dataframe. Get the Sheet_names, and then extract the sheet_name that has 'ITD_'.

excel = pd.ExcelFile("your_excel.xlsx")
excel.sheet_names
# ["Sheet1", "Sheet2"]
for n in excel.sheet_names:
    if n.startswith('ITD_'):
        sheetname = n
        break
df = excel.parse(sheetname)
Veris
  • 26
  • 3