Home » excel » how to import the excel tabs and give the name of tabs in new column accordingly in python?

how to import the excel tabs and give the name of tabs in new column accordingly in python?

Posted by: admin May 14, 2020 Leave a comment


enter image description here I have file named Example.xls in which i have data in tab sales and purchase.
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

all= pd.read_excel(Example.xlsx',sheet_name=['Sales','Purchas'])
enter image description here
and then

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

How to&Answers:

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 = \
           for f in glob(f_mask)
           for sheet, df in pd.read_excel(f, sheet_name=None).items()],

The code works in following way:

  1. Check the base folder and take all the .xlsx files in it
  2. Read the files one by one
  3. 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