简体   繁体   中英

Loading data from excel and appending all sheets

I loaded 15 sheets from excel sheet and it created dictionary of dataFrames with sheet number as key. Is there a way to actually append 15 sheets while loading or I have to iterate over keys to append 15 DataFrames?

I mean while loading specifying to append instead of creating dictionary?

df = pd.read_excel(r"E:\user_ratings.xlsx",sheetname=range(1,16),skiprows=28,
                                       header=None,parse_cols="D:H")

df.head()
AttributeError: 'dict' object has no attribute 'head'

df.keys()
Out[113]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

If list a value of key 1 it gives me dataFrame of sheet 1.

df[1]
Out[114]: 
EVE01201                  ......

If you look at the read_excel docs the sheetname parameter seems to only give you the option of returning a Dict of DataFrames, with keys representing sheets. The only way I could see would be to modify the excel.py file used by pandas (excel.py uses xlrd).

But it would probably be a lot simpler to simply concatenate the dataframes using pd.concat

As far as I know by using ExcelFile, it would be as simple as :

import pandas as pd
df = pd.DataFrame()
input_excel = pd.ExcelFile(path/file.xlsx)  

#iterate over each sheet in the file and parse it  

for sheet in input_excel.sheet_names:
    input_sheet = input_excel.parse(sheet,headers = True)  
    df.append(input_sheet)

sheet_names is an attribute to get all the sheet names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM