How to import an Excel file into Pandas

Published on Aug. 22, 2023, 12:12 p.m.

You can easily import an Excel file into Pandas using Pandas.

import pandas as pd

df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
print(df)

Steps to Import an Excel File into Pandas using Pandas.

Suppose that you have the following table in Excel (where the Excel file name is ‘product ‘):

Step 1: Capture the file path

In the Python code below, you’ll need to modify the path name.

Step 2: Apply the Python code

import pandas as pd

df = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx')
print(df)

Note that you should place “r” before the path string to addressed special characters.

Step 3: Run the Python code to import the spreadsheet file .

Run the code (adjusted to your path), and you’ll get the following row:

  product_name  price
0     computer    700
1       tablet    250
2      printer    120
3       laptop   1200
4     keyboard    100

Note: you’ll have to install an additional package if you get the following error.

ImportError: Missing optional dependency ‘xlrd’

pip install openpyxl

Optional step : selecting subset of columns.

Now what if you want to select a specific column or columns from the spreadsheet file .

import pandas as pd

data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
df = pd.DataFrame(data, columns=['product_name'])
print(df)

Run the code after adjusting the file path. You will get only the product name column.

  product_name
0     computer
1       tablet
2      printer
3       laptop
4     keyboard

You can specify additional columns by separating their names using a comma.

import pandas as pd

data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
df = pd.DataFrame(data, columns=['product_name', 'price'])
print(df)

You saw how to import an Excel file into Pandas.