How to Import an Excel File in Python

In today’s world, data handling is an integral part of every business. Entities often store data in Excel due to its ease of usage and wide range of functionalities.

If you’re working with Python, it’s pretty common practice to import data from Excel files for further data analysis or data visualization. In this guide, we are going to walk through the steps on how to import an Excel file with Python.

Step 1: Create an Excel File

Step 2: Install the Necessary Libraries

Python, by default, does not come with the necessary libraries for handling Excel files. But, don’t worry. You can simply install it via the command line. Open your command line or terminal and type the following command:

Here, pandas is a powerful data analysis and manipulation library and xlrd is a library for reading data and formatting information from Excel files, whether they are .xls or .xlsx files.

Step 3: Import the Libraries in Python

After installing the required libraries, the next step is to import these libraries to our Python script. Type the following code:

Step 4: Load the Excel File

Now, let’s load the file. We are going to use the ‘read_excel’ function from the pandas library to load our Excel file. Here is the syntax:

In this line of code, ‘filename.xlsx’ is the name of your Excel file. Ensure that the file is located in the same directory as your Python script, or you can also mention the exact file location.

Step 5: Displaying the Data

Once you have read the data, you can display it using the print function. Here’s how:

This will display the content of the Excel file in the console.

Here is the Full Python Code:

The output of the code should be:

   ID Product  Sales
0   1   Apple    100
1   2  Banana    200
2   3  Orange    150
3   4   Mango    300
4   5  Grapes    220

Conclusion

In this tutorial, we have discussed how to import an Excel file in Python. We have walked through the steps of how to install the necessary libraries, load the Excel file, and display the data from the file.