In this section, we are going to read Excel files using the pandas module. Now, let's look at an example of reading an Excel file.
Create a script called rd_excel_pandas.py and write the following content in it:
import pandas as pd
excel_file = 'sample.xlsx'
df = pd.read_excel(excel_file)
print(df.head())
Run the preceding script and you will get the following output:
student@ubuntu:~/test$ python3 rd_excel_pandas.py
Following is the output:
OrderDate Region ... Unit Cost Total
0 2014-01-09 Central ... 125.00 250.00
1 6/17/15 Central ... 125.00 625.00
2 2015-10-09 Central ... 1.29 9.03
3 11/17/15 Central ... 4.99 54.89
4 10/31/15 Central ... 1.29 18.06
In the preceding example, we are reading an Excel file using the pandas module. First, we imported the pandas module. Then, we created a string called excel_file to hold the name of the file to be opened, which we want to manipulate using pandas. Later on, we created a df data frame object. In this example, we used the read_excel method of pandas to read data from the Excel file with default functions. The reading starts with index zero. Finally, we printed the pandas data frame.