How to read a .xlsx file using the pandas Library?

Given a .xlsx file, we have to read it using the pandas Library. By Pranit Sharma Last updated : September 22, 2023

Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and the data.

Pandas DataFrames can be created with the help of dictionaries or arrays but in real-world analysis, first, a CSV file or an xlsx file is imported and then the content of CSV or excel file is converted into a DataFrame.

An xlsx file is a spreadsheet file that can be created with certain software, especially with the popularly known MS Excel. To import and read an Excel file using the pandas.ExcelFile() method, use the following piece of code:

pd.ExcelFile(Path_of_the_file')
Note

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python program to read a .xlsx file using the pandas Library

# Importing pandas package
import pandas as pd

# Reading an xlsx file
data = pd.read_excel("D:/Book1.xlsx")

# Display data
print("Records from the Excel sheet:\n",data)

Output

The output of the above program is:

Example: Read a .xlsx file

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.