Python用pandas進行大數(shù)據(jù)Excel兩文件比對去重
背景介紹:
通俗理解有兩個excel文件 分別為A和B
我要從B中去掉A中含有的數(shù)據(jù),數(shù)據(jù)量大約在300w左右
因為數(shù)據(jù)量較大,無論是wps還是office自帶的去重都無法正常使用這樣就需要用到腳本了
話不多說,代碼如下:
import pandas as pd
from tqdm import tqdm
# 引號內填寫需要去重的表格路徑
targetExcel = r'./222.xlsx'
# 引號內填寫依據(jù)表格的路徑
basisExcel = r'./11.xlsx'
# 引號內填寫輸出字段
field = 'removeRepeatResult'
def removeRepeat():
count = 0
ind = 1
targetIndex = field + str(ind)
resultExcel = {
field+'1': []
}
header = ['A','B','C','D','E','F','G','H','I','J','K']
print('讀取數(shù)據(jù)')
target_Excel = pd.read_excel(targetExcel,header=None,names=header, dtype='object')
basis_Excel = pd.read_excel(basisExcel,header=None,names=['A'], dtype='object')
print('讀取成功')
for index in tqdm(header):
for i in tqdm(target_Excel[index], leave=False):
if pd.isnull(i):
continue
elif i in list(basis_Excel['A']):
continue
else:
resultExcel[targetIndex].append(i)
count += 1
if count >= 1020000:
count = 0
ind += 1
targetIndex = field + str(ind)
resultExcel[targetIndex] = []
print('等待數(shù)據(jù)合并')
df = pd.concat([pd.DataFrame(i) for i in resultExcel.values()], axis=1)
df.fillna(0) # 取消長短不一致問題
df.to_excel('resultExcel.xlsx', header=None, index=False) # 取消表頭與行號
#上一行中自定義文件名!
removeRepeat()
input('>>> 任意鍵退出...')
運行效果圖:
文章來源:http://www.zghlxwxcb.cn/news/detail-617503.html
?歡迎大家指導交流,共同學習,共同進步!文章來源地址http://www.zghlxwxcb.cn/news/detail-617503.html
到了這里,關于Python用pandas進行大數(shù)據(jù)Excel兩文件比對去重300w大數(shù)據(jù)處理的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!