How to Import an Excel File into Python Using Pandas?

There are various functions and modules in Python that are used to deal with files in Python such as csv module, pandas, os, etc. The panda’s module provides various functions to perform different kinds of tasks, such as to create a data frame in Python, the “pd.DataFrame()” function is used, to convert a dictionary to csv, the “pd.DataFrame.from_dict()” function is used in Python, etc.

This post will address the method to import an Excel file into Python using Pandas. 

Using Pandas Module to Import an Excel File into Python

You can use the “pd.read_excel()” function to import an excel file into Python. The specific content and complete excel file data can be imported into Python using this function. Let’s understand the below examples:

Example 1: Using the “pandas.read_excel()” Function to Read an Excel File 

The original excel file named “ILF.xlsx” contains the following content and is placed in the Python working directory.

The “pd.read_excel()” function is used in the below example to read an excel file.

Code:

import pandas
output = pandas.read_excel(r'ILF.xlsx')
print(output)

In the above code, the “pandas.read_excel()” function takes the file name along with the excel format to read the excel data and returns the data in a well-organized way.

Output:

The above snippet displays the “Content” of the excel file named “ILF.xlsx”

Example 2: Read Specific Column From the Excel File Using DataFrame Columns Parameter

In the below code, the “pd.read_excel()” and “pd.DataFrame()” function is used to read only the specific columns from the given excel file:

Code:

import pandas
x = pandas.read_excel(r'ILF.xlsx')
output = pandas.DataFrame(x, columns=['Name','Team'])
print(output)

In the above code, the “pd.read_excel()” reads the content of the excel file named “ILF.xlsx”. Next, the result retrieved by the read_excel() function is passed to the “pd.DataFrame()” function as an argument. The “pd.DataFrame()” function returns the specific columns’ data based on the column names specified in the “columns” parameter.

Output: 

The above snippet displays the specific columns’ data.

Example 3: Read an Excel File and Change the Specific Column’s Datatype Using the “dtype”

In the code example given below, the excel file is read, and the data type of the specific columns is changed using the “dtype” parameter:

Code:

import pandas
output = pandas.read_excel('ILF.xlsx', dtype={"ID NO:": float})
print(output)

    In the above code, the “pd.read_excel()” function takes the name and format of the excel file as a first argument. The second parameter, “dtype” takes the value for the column name along with the return data type: “float”.

    Output:

    The above output shows that the data type of the “ID NO:” column has been changed from an integer to a floating point.

    Conclusion

    To import an excel file, the “pd.read_excel()” function of the panda’s module is used in Python. We can also read the specific column from the excel file utilizing the “pd.read_excel()” function and the “pd.DataFrame()” function. The data type of any individual column can be changed using the “dtype” parameter of the “pd.read_excel()” function. This guide taught us how to import an excel file in Python using pandas.