Excel is one of the most common formats for tabular data. With this in mind, pandas
provides us with some helpful methods to work with Excel. In this article, we will see how to read an Excel file using pandas
.
To use Excel files with pandas, we will first need the xlrd
library. If you don't have this, pandas
will warn you.
pip install xlrd
Now, to read an Exexl file, we use the read_excel
method from pandas. Here is how we can create a datframe from Excel.
import pandas as pd
df = pd.read_excel('data.xlsx')
print(df.head())
If the Excel file has multiple sheets, you can do two thigs. First, you can use the above code and access a sheet by using the sheet name as a key.
print(df['sheet1'])
You can also read the sheet directly and skip other sheets by supplying the sheet_name
parameter to the read_excel
method.
import pandas as pd
df = pd.read_excel('data.xlsx', sheet_name='sheet1')
print(df.head())