Examples of how to read an excel file (with extension xlsx) with pandas in python
Read a excel file with only one sheet
Let's consider the following excel file:

To read a excel file with pandas, a solution is to use the the pandas function read_excel()
import pandas as pddf = pd.read_excel ('../read_excel_file_with_python.xlsx')print (df)
gives
First Name Last Name Age Height0 John Doe 24 1901 Elijah Baley 31 1692 Paul Edison 22 1853 Martin Cage 41 1764 Robert Lemon 32 195
Note: If the following message error appears
Missing optional dependency 'xlrd'. Install xlrd >= 1.0.0 for Excel support Use pip or conda to install xlrd.
it is necessary to install first the module xlrd. For example with conda:
conda install xlrd
Read a excel file with several sheets
Let's onsider for example a excel file with two sheets:
To read the excel file, use first the function ExcelFile():
xls = pd.ExcelFile('../read_excel_file_with_python.xlsx')
and get the sheet names:
print(xls.sheet_names)
returns
['Personal Data', 'Public Data']
Then get data from the sheet 1
df1 = pd.read_excel(xls, 'Personal Data')print(df1)
returns
First Name Last Name Age Height0 John Doe 24 1901 Elijah Baley 31 1692 Paul Edison 22 1853 Martin Cage 41 1764 Robert Lemon 32 195
and sheet 2
df2 = pd.read_excel(xls, 'Public Data')print(df2)
returns
id pseudo0 1 Dodo1 2 Space2 3 Edi3 4 Azerty4 5 Bob
