enter image description here I have file named
Example.xls in which i have data in tab
We have data in both tab from Column A to E.
When i import these data through pandas module, i want that result like Column A to F where column F should display the name sheet name. How to display the name of sheet name in pandas module?
I am using code
enter image description here
df= pd.concat(All[frame]for fram in All.keys())
and then after i want to put the name of tabs in my data frame “All” in the last column which is F respectively
I think this is the simplest way.
import pandas as pd path = r'path_of_your_file' workbook = pd.read_excel(path, sheet_name = None) df= pd.DataFrame() for sheet_name, sheet in workbook.items(): sheet['sheet'] = sheet_name df = df.append(sheet) # Reset your index or you'll have duplicates df = df.reset_index(drop=True)
The below code will solve your problem:
import os from glob import glob import pandas as pd f_mask = r'path\*.xlsx' ## The folder path where your Example.xlsx is stored df = \ pd.concat([df.assign(file=os.path.splitext(os.path.basename(f)), sheet=sheet) for f in glob(f_mask) for sheet, df in pd.read_excel(f, sheet_name=None).items()], ignore_index=True)
The code works in following way:
- Check the base folder and take all the .xlsx files in it
- Read the files one by one
- Make two additional columns, one for file name other for sheet name
This solution will work if you want to do the exercise for more than 1 .xlsx file