Import several sheets from the same excel into one dataframe in pandas

PV8 :

I have one excel file with several identical structured sheets on it (same headers and number of columns) (sheetsname: 01,02,...,12).

How can I get this into one dataframe?

Right now I would load it all seperate with:

df1 = pd.read_excel('path.xls', sheet_name='01')
df2 = pd.read_excel('path.xls', sheet_name='02')
...

and would then concentate it. What is the most pythonic way to do it and get directly one dataframe with all the sheets? Also assumping I do not know every sheetname in advance.

sammywemmy :

read the file as:

collection =  pd.read_excel('path.xls', sheet_name=None)

combined = pd.concat([value.assign(sheet_source=key)
                      for key,value in collection.items()],
                     ignore_index=True)

sheet_name = None ensures all the sheets are read in.

collection is a dictionary, with the sheet_name as key, and the actual data as the values.

combined uses the pandas concat method to get you one dataframe.

I added the extra column sheet_source, in case you need to track where the data for each row comes from.

You can read more about it on the pandas doco

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12381&siteId=1