Home »
Python »
Python Programs
How to read a .xlsx file using the pandas Library?
Given a .xlsx file, we have to read it using the pandas Library.
Submitted by Pranit Sharma, on June 11, 2022
effectively and efficiently. 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 an Excel file, we use the following piece of code:
pd.ExcelFile(Path_of_the_file')
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,
# 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:
Python Pandas Programs »