📜  通过 Excel 获取数据:比较运行时

📅  最后修改于: 2022-05-13 01:55:06.363000             🧑  作者: Mango

通过 Excel 获取数据:比较运行时

数据摄取是获取和导入数据以存储在数据库中的过程。在本文中,我们探讨了用于从Python中的 excel 文件中提取数据并比较它们的运行时的不同数据摄取技术。

让我们假设 excel 文件看起来像这样——

数据摄取-python

使用 xlrd 库

使用xlrd模块,可以从电子表格中检索信息。例如,可以在Python中读取、写入或修改数据。此外,用户可能必须浏览各种工作表并根据某些标准检索数据或修改某些行和列并做很多工作。

import xlrd
import time
  
  
# Time variable for finding the 
# difference
t1 = time.time()
  
#Open the workbook to read the
# excel file 
workbook = xlrd.open_workbook('excel.xls')
   
#Get the first sheet in the workbook 
sheet = workbook.sheet_by_index(0)
  
#Read row data line by line 
for i in range(sheet.nrows):
    row = sheet.row_values(i) 
    print(row)
      
t2 = time.time()
print("\nTime taken by xlrd:")
print(t2-t1)

输出:

数据摄取-xlrd

使用熊猫

Python数据分析库是数据科学家使用的强大工具。它有助于数据摄取和数据探索。

import pandas as pd 
import time
  
  
# Time variable for finding the 
# difference
t1 = time.time()
  
data = pd.read_excel('excel.xls') 
print(data.head())
  
t2 = time.time()
print("\nTime taken by xlrd:")
print(t2-t1)

输出:

数据摄取-pandas1

使用 dask 数据框

Dask DataFrame 是一个大型并行 DataFrame,由许多较小的 Pandas DataFrames 组成,沿索引拆分。

import dask
import dask.dataframe as dd
import pandas as pd 
from dask.delayed import delayed
import time
  
  
# Time variable for finding the 
# difference
t1 = time.time()
  
   
parts = dask.delayed(pd.read_excel)('excel.xls', 
                                    sheet_name=0)
df = dd.from_delayed(parts)
   
print(df.head())
  
t2 = time.time()
print("\nTime taken by Dask:")
print(t2-t1)

输出:

数据摄取-dask