How to Import an Excel File Into Python using pandas
In this tutorial, you will import an Excel file using Python and pandas.
TLDR solution
import pandas as pd
df = pd.read_excel("path-to-file/file-name.xlsx")
Step-by-Step Example
Step 1: Install the pandas and openpyxl
If you don't have the packages pandas and openpyxl already installed, execute the following command in your terminal:
pip install pandas openpyxl
Note that we will use the pandas function read_excel below which depends on openpyxl.
Step 2: Import an Excel file
Let's say, you have an excel file named fish.xlsx on your desktop that contains the following data:
| fish_name | egg_count |
|---|---|
| salmon | 5,000 |
| pufferfish | 200 |
| shark | 2 |
Create a Python script that loads the excel file into a pandas DataFrame and prints it to the terminal:
import os
import pandas as pd
desktop_path = os.path.expanduser("~/Desktop")
df = pd.read_excel(desktop_path + "/fish.xlsx")
print(df)
Note that if the excel file contains multiple sheets, you have to specify the sheet that you want to import:
import os
import pandas as pd
desktop_path = os.path.expanduser("~/Desktop")
df = pd.read_excel(desktop_path + "/fish.xlsx", sheet_name="Sheet1")
print(df)
Now if you run the script, you should see the following output in your terminal:
fish_name egg_count
0 salmon 5000
1 pufferfish 200
2 shark 2
That's it! You just learned how to import an Excel file using Python and pandas.