↑ 點擊上方 “一行數據” 關注 + 星標 ~
每週送書,絕不錯過
之前分享過python調用過ppt和word,作爲一家人的excel當然要整整齊齊的安排上
相對於excel,已經有人都寫成了一本書。這裏一篇文檔根本寫不下,但是行哥想起來若干年前,在處理數據的時候最大的難題就是導入excel數據,因爲後來的數據清洗,提取都可以一步步來做。但是數據導入因爲教程不一,文字編碼不一,着實快成爲我從入門到放棄的第一塊門檻
所以本文介紹三種強大的python模塊來讀取excel,選用案例是之前分享過的分析2020年12000條python招聘數據,有興趣的可以點擊這裏看一下
1.pandas
matplotlib、numpy、pandas是入行數據分析的三個必須掌握的基礎模塊,這裏介紹一下用pandas如何導入excel文件。安裝比較簡單,直接用 pip 工具安裝三個庫即可,安裝命令如下:
$ pip3 install pandas
安裝完成提示 Successfully installed即表示安裝成功。
# 1.導入pandas模塊
import pandas as pd
# 2.把Excel文件中的數據讀入pandas
df = pd.read_excel('Python招聘數據(全).xlsx')
print(df)
# 3.讀取excel的某一個sheet
df = pd.read_excel('Python招聘數據(全).xlsx', sheet_name='Sheet1')
print(df)
# 4.獲取列標題
print(df.columns)
# 5.獲取列行標題
print(df.index)
# 6.制定打印某一列
print(df["工資水平"])
# 7.描述數據
print(df.describe())
其中的describe函數可以統計整體工資情況,告訴行哥你有沒有超過50%
使用for循環遍歷整個excel文件,我們可以看到12000行數據總耗時達到2.6s
import time
t1 = time.time()
for indexs in df.index:
print(df.loc[indexs].values[0:-1])
t2=time.time()
print("使用pandas工具包遍歷12000行數據耗時:%.2f 秒"%(t2-t1))
2.openpyxl
小五說這個最好用的python 操作 excel 表格庫,下面可以看到openpyxl的讀取方法。安裝比較簡單,直接用 pip 工具安裝三個庫即可,安裝命令如下:
$ pip3 install openpyxl
安裝完成提示 Successfully installed即表示安裝成功。
from openpyxl import load_workbook
# 1.打開 Excel 表格並獲取表格名稱
workbook = load_workbook(filename="Python招聘數據(全).xlsx")
print(workbook.sheetnames)
# 2.通過 sheet 名稱獲取表格
sheet = workbook["Sheet1"]
print(sheet)
# 3.獲取表格的尺寸大小(幾行幾列數據) 這裏所說的尺寸大小,指的是 excel 表格中的數據有幾行幾列,針對的是不同的 sheet 而言。
print(sheet.dimensions)
# 4.獲取表格內某個格子的數據
# 1 sheet["A1"]方式
cell1 = sheet["A1"]
cell2 = sheet["C11"]
print(cell1.value, cell2.value)
"""
workbook.active 打開激活的表格; sheet["A1"] 獲取 A1 格子的數據; cell.value 獲取格子中的值;
"""
# 4.2sheet.cell(row=, column=)方式
cell1 = sheet.cell(row = 1,column = 1)
cell2 = sheet.cell(row = 11,column = 3)
print(cell1.value, cell2.value)
# 5. 獲取一系列格子
# 獲取 A1:C2 區域的值
cell = sheet["A1:C2"]
print(cell)
for i in cell:
for j in i:
print(j.value)
通過openpyxl庫操作excel,使用for循環迭代打印12000行數據僅需要0.47 s
import time
t1 = time.time()
for i in sheet.iter_rows(min_row=1, max_row=12256, min_col=1, max_col=10):
for j in i:
print(j.value)
t2=time.time()
print("使用openpyxl工具包遍歷12000行數據耗時:%.2f 秒"%(t2-t1))
3.xlrd
xlrd是xlrd&xlwt&xlutils三個庫中的一個:
xlrd:用於讀取 Excel 文件;xlwt:用於寫入 Excel 文件;xlutils:用於操作 Excel 文件的實用工具,比如複製、分割、篩選等;
安裝比較簡單,直接用 pip 工具安裝三個庫即可,安裝命令如下:
$ pip3 install xlrd xlwt xlutils
安裝完成提示 Successfully installed xlrd-1.2.0 xlutils-2.0.0 xlwt-1.3.0 即表示安裝成功。
接下來我們就從寫入 Excel 開始,話不多說直接看代碼如下:
# 導入 xlrd 庫
import xlrd
# 打開剛纔我們寫入的 test_w.xls 文件
wb = xlrd.open_workbook("Python招聘數據(全).xlsx")
# 獲取並打印 sheet 數量
print( "sheet 數量:", wb.nsheets)
# 獲取並打印 sheet 名稱
print( "sheet 名稱:", wb.sheet_names())
# 根據 sheet 索引獲取內容
sh1 = wb.sheet_by_index(0)
# 也可根據 sheet 名稱獲取內容
# sh = wb.sheet_by_name('成績')
# 獲取並打印該 sheet 行數和列數
print( u"sheet %s 共 %d 行 %d 列" % (sh1.name, sh1.nrows, sh1.ncols))
# 獲取並打印某個單元格的值
print( "第一行第二列的值爲:", sh1.cell_value(0, 1))
# 獲取整行或整列的值
rows = sh1.row_values(0) # 獲取第一行內容
cols = sh1.col_values(1) # 獲取第二列內容
# 打印獲取的行列值
print( "第一行的值爲:", rows)
print( "第二列的值爲:", cols)
# 獲取單元格內容的數據類型
print( "第二行第一列的值類型爲:", sh1.cell(1, 0).ctype)
# # 遍歷所有表單內容
import time
t1 = time.time()
for sh in wb.sheets():
for r in range(sh.nrows):
# 輸出指定行
print( sh.row(r))
t2=time.time()
print("使用xlrd工具包遍歷12000行數據耗時:%.2f 秒"%(t2-t1))
5.總結
類型 | xlrd&xlwt&xlutils | pandas | OpenPyXL |
---|---|---|---|
讀取 | 支持 | 支持 | 支持 |
寫入 | 支持 | 支持 | 支持 |
修改 | 支持 | 支持 | 支持 |
xls | 支持 | 支持 | 不支持 |
xlsx | 高版本支持 | 支持 | 支持 |
大文件 | 不支持 | 支持 | 支持 |
效率 | 快 | 快 | 快 |
功能 | 較弱 | 強大 | 一般 |
遍歷耗時 | 0.35 s | 2.60 s | 0.47 s |
這裏附上3個模塊的性能對比,從遍歷時間上xlrd模塊最快,從功能強大上我選擇pandas,從數據量上我得選擇mysql、hadoop、spark????
往期推薦
對了,可以加下行哥微信好友,私聊回覆「02」可以領取5T編程資料哦
人生苦短,我用Python
祝三連的讀者這個月找到對象!!!!!