Fastest Way to Read Excel File in Python
Last Updated :
03 Dec, 2024
Reading Excel files is a common task in data analysis and processing. Python provides several libraries to handle Excel files, each with its advantages in terms of speed and ease of use. This article explores the fastest methods to read Excel files in Python.
Using pandas
pandas is a powerful and flexible data analysis library in Python. It provides the read_excel function to read Excel files. While not the fastest, it is highly efficient and widely used for its ease of use and versatility.
Python
import pandas as pd
# Read Excel file
df = pd.read_excel('data.xlsx')
print(df.head())
Output:
Output
pandas.read_excel can handle large datasets efficiently and supports various Excel formats. It can also read multiple sheets by specifying the sheet_name parameter.
Note: Use usecols to load only specific columns. Use nrows to limit the number of rows read. Specify dtype for columns to avoid type inference overhead.
Using openpyxl
openpyxl
is another popular library for reading and writing Excel files. It is particularly useful for working with .xlsx
files.
Python
from openpyxl import load_workbook
# Load workbook
wb = load_workbook('data.xlsx', read_only=True)
# Select a sheet
ws = wb['Sheet1']
# Read data
li = []
for i in ws.iter_rows(values_only=True):
li.append(i)
print(li[:5])
Output:
[('Topic', 'Description', 'Difficulty Level', 'Link'),
('Python', 'Learn Python programming language.', 'Beginner', 'https://www.geeksforgeeks.org/python-programming-language/'),
('Data Structures', 'Study of data structures', 'Intermediate', 'https://www.geeksforgeeks.org/data-structures/'),
('Algorithms', 'Learn various algorithms', 'Advanced', 'https://www.geeksforgeeks.org/fundamentals-of-algorithms/'),
('Machine Learning', 'Introduction to Machine Learning ', 'Intermediate', 'https://www.geeksforgeeks.org/machine-learning/')]
openpyxl provides fine-grained control over reading and writing Excel files. The read_only mode significantly improves performance when reading large files.
Using xlrd for .xls format
xlrd is a library for reading data and formatting information from Excel files in the historical .xls format. For newer .xlsx files, consider using openpyxl or pandas.
Python
import xlrd
# Open workbook
wb = xlrd.open_workbook('data.xls')
# Select a sheet
sheet = wb.sheet_by_name('Sheet1')
# Read data
li = []
for idx in range(sheet.nrows):
row = sheet.row(idx)
li.append([cell.value for cell in row])
print(li[:5])
Output
[['Topic', 'Description', 'Difficulty Level', 'Link'],
['Python', 'Learn Python programming language.', 'Beginner', 'https://www.geeksforgeeks.org/python-programming-language/'],
['Data Structures', 'Study of data structures', 'Intermediate', 'https://www.geeksforgeeks.org/data-structures/'],
['Algorithms', 'Learn various algorithms', 'Advanced', 'https://www.geeksforgeeks.org/fundamentals-of-algorithms/'],
['Machine Learning', 'Introduction to Machine Learning', 'Intermediate', 'https://www.geeksforgeeks.org/machine-learning/']]
Using pyxlsb for .xlsb Binary Excel Format
pyxlsb is a library for reading Excel files in the Binary Excel format (.xlsb). It is significantly faster for large files compared to other libraries.
Python
import pyxlsb
# Read .xlsb file
with pyxlsb.open_workbook('data.xlsb') as wb:
with wb.get_sheet(1) as sheet:
li = []
for i in sheet.rows():
li.append([item.v for j in row])
print(li[:5])
Output
[['Topic', 'Description', 'Difficulty Level', 'Link'],
['Python', 'Learn Python programming language.', 'Beginner', 'https://www.geeksforgeeks.org/python-programming-language/'],
['Data Structures', 'Study of data structures', 'Intermediate', 'https://www.geeksforgeeks.org/data-structures/'],
['Algorithms', 'Learn various algorithms', 'Advanced', 'https://www.geeksforgeeks.org/fundamentals-of-algorithms/'],
['Machine Learning', 'Introduction to Machine Learning', 'Intermediate', 'https://www.geeksforgeeks.org/machine-learning/']]
pyxlsb is optimized for reading binary Excel files, which can be much faster than other formats for large datasets.
Similar Reads
How to Convert Tab-Delimited File to Csv in Python? We are given a tab-delimited file and we need to convert it into a CSV file in Python. In this article, we will see how we can convert tab-delimited files to CSV files in Python. Convert Tab-Delimited Files to CSV in PythonBelow are some of the ways to Convert Tab-Delimited files to CSV in Python: U
2 min read
Python program to read CSV without CSV module CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the c
3 min read
Convert Excel To Json With Python In the realm of data manipulation and analysis, the ability to convert data between different formats is a valuable skill. Excel and JSON are two widely used formats, and Python, with its powerful libraries, provides a seamless way to convert Excel files into JSON. In this article, we will see how w
4 min read
Python | Test if number is valid Excel column Sometimes, while working with Python strings, we can have a problem in which we need to test for string if it's a valid Excel column. This has application in many domains including day-day programming, web development, and Data Science. Let us discuss certain ways in which this task can be performed
3 min read
How to Append Data in Excel Using Python We are given an Excel file and our task is to append the data into this excel file using Python. In this article, we'll explore different approaches to append data to an Excel file using Python. Append Data in Excel Using PythonBelow, are some examples to understand how to append data in excel using
2 min read
Closing an Excel File Using Python We are given an excel file that is opened and our task is to close that excel file using different approaches in Python. In this article, we will explore three different approaches to Closing Excel in Python. Closing an Excel Session with PythonBelow are the possible approaches to Using Os In Python
2 min read