Reading multiple excel files from a certain path (with certain sheets names) into a single pandas data frame

Question:

How can I read only sheets ‘2g’,’3g’,’4g’ from all the excel files exists in a certain path and write them to three different data frames.
All 2g sheets should be stored in a pandas data frame called (2g)
All 3g sheets should be stored in a pandas data frame called (3g)
All 4g sheets should be stored in a pandas data frame called (4g)
following the screenshot of the excel files :
enter image description here

and the sheets I want to read from each excel files as in the screen below :

enter image description here

Asked By: Jalal Hasain

||

Answers:

You can use read_excel with multiple sheet names:

import pandas as pd

data2g = []
data3g = []
data4g = []

for xlsx in pathlib.Path('mobile').glob('*.xlsx'):
    df2g, df3g, df4g = pd.read_excel(xlsx, sheet_name=['2G', '3G', '4G']).values()
    data2g.append(df2g)
    data3g.append(df3g)
    data4g.append(df4g)

df2g = pd.concat(data2g, ignore_index=True)
df3g = pd.concat(data3g, ignore_index=True)
df4g = pd.concat(data4g, ignore_index=True)

As you can see, you can improve readability using loops:

import pandas as pd
from collections import defaultdict

data = defaultdict(list)
for xlsx in pathlib.Path('mobile').glob('*.xlsx'):
    out = pd.read_excel(xlsx, sheet_name=['2G', '3G', '4G'])
    for k, v in out.items():
        data[k].append(v)
df2g, df3g, df4g = [pd.concat(data[k], ignore_index=True) for k in data]
Answered By: Corralien
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.