Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read multiple tables from .xls file in python?

I need to read multiple tables from a sheet in an Excel file with python. The sheet looks something like this: enter image description here

I want to get a python object containing the information in First_Table and the same for Second_Table. I tried using pandas and Dataframe.iloc this way:

import pandas as pd
xls = pd.ExcelFile('path_to_xls_file')
df = pd.read_excel(xls, "sheet_1")
# first table
df1 = df.iloc[2:12,0:6]

But I didn't get the expected cells from the First_Table. Am I doing something wrong with the ranges of the rows and columns? Does it have to be specified with the exact row and col indices or is there a more efficient and elegant way to do it?

Thanks in advance!

like image 937
Ori Netanel Ben-Zaken Avatar asked Jan 09 '19 09:01

Ori Netanel Ben-Zaken


1 Answers

Use "usecols" argument to select the columns you want to read from excel file. Pandas will select the rows accordingly.

Also you need to set index to False to avoid getting first column as index.

Following is the example code for your task

pd.read_excel(path, usecols=range(1,6), index=False)

Find more information in documentation

like image 159
Keval Dave Avatar answered Nov 14 '22 23:11

Keval Dave