How to Import an Excel File into Python using Pandas

To import an Excel file into Python using Pandas, you can use the read_excel() function as shown below:

import pandas as pd

df = pd.read_excel(r"Full\Path\To\Your\File\products_data.xlsx")

print(df)

If you need to import a specific worksheet from the Excel file, you can specify the sheet name:

import pandas as pd

df = pd.read_excel(r"Full\Path\To\Your\File\products_data.xlsx", sheet_name="Sheet1")

print(df)

Steps to Import an Excel File

Step 1: Install the Required Packages

If you have not yet installed the required libraries, you need to install both Pandas and Openpyxl.

To install Pandas:

pip install pandas

To install Openpyxl:

pip install openpyxl

Pandas handles the data manipulation, while Openpyxl allows Python to read .xlsx Excel files.

Step 2: Save the Data in an Excel File

Suppose you have the following table stored in an Excel file:

product_nameprice
computer720
tablet280
printer150
laptop1250
keyboard110

Save the Excel file as “products_data.xlsx”.

Make sure the file extension is .xlsx, as this is the standard Excel format supported by Openpyxl.

Step 3: Capture the File Path

Next, capture the full path to the Excel file on your computer.

For example:

C:\Users\Alex\Documents\products_data.xlsx

Where:

  • products_data is the file name
  • .xlsx is the file extension

You will need to adjust the file path in your Python script to match your system.

Step 4: Apply the Python Script

Here is the Python script based on the example above:

import pandas as pd

df = pd.read_excel(r"C:\Users\Alex\Documents\products_data.xlsx")

print(df)

Notice that the letter r is placed before the path string. This ensures that special characters such as backslashes (\) are handled correctly.

Step 5: Run the Python Script

After adjusting the path, run the script. You should receive the following output:

  product_name  price
0     computer    720
1       tablet    280
2      printer    150
3       laptop   1250
4     keyboard    110

The output matches the dataset stored in the Excel file. Pandas automatically loads the data into a DataFrame and assigns an index starting from 0.

Optional Step: Selecting a Subset of Columns

If you want to select specific columns from the Excel file, you can do so by explicitly listing the column names.

For example, to select only the product_name column:

import pandas as pd

data = pd.read_excel(r"C:\Users\Alex\Documents\products_data.xlsx")

df = pd.DataFrame(data, columns=["product_name"])

print(df)

After running the script, you will see:

  product_name
0     computer
1       tablet
2      printer
3       laptop
4     keyboard

If you want to include both product_name and price, you can separate the column names with a comma:

import pandas as pd

data = pd.read_excel(r"C:\Users\Alex\Documents\products_data.xlsx")

df = pd.DataFrame(data, columns=["product_name", "price"])

print(df)

Ensure that the column names in the code exactly match those in the Excel file. Any mismatch in spelling or capitalization may result in NaN values or errors.

Conclusion

You have now learned how to import an Excel file into Python using Pandas. This is a common task in data analysis, reporting, and automation workflows.

In many situations, you may also need to import CSV files into Python using a similar approach with read_csv(). Understanding both methods allows you to work confidently with structured data files.

You can further explore the different options available in the read_excel() function to control sheet selection, header rows, data types, and more, depending on your project requirements.

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments