偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python兩個Excel多Sheet數(shù)據(jù)對比

開發(fā) 前端
在數(shù)據(jù)處理與分析中,經(jīng)常需要比較來自不同來源的數(shù)據(jù)集,特別是在處理涉及多個Excel工作簿和工作表的場景時。

在數(shù)據(jù)處理與分析中,經(jīng)常需要比較來自不同來源的數(shù)據(jù)集,特別是在處理涉及多個Excel工作簿和工作表的場景時。Python的Pandas庫提供了強大的工具,可以幫助我們高效地完成這一任務。下面,我們將一步步引導你如何使用Python對比兩個Excel文件中多個Sheet的數(shù)據(jù)。

第一步:安裝必要的庫

確保你的Python環(huán)境中已安裝pandas和openpyxl。如果沒有安裝,可以通過以下命令安裝:

pip install pandas openpyxl

第二步:讀取Excel文件中的多個Sheet

使用pandas.ExcelFile或pandas.read_excel直接讀取多個Sheet的數(shù)據(jù)。

import pandas as pd
# 讀取第一個Excel文件的所有Sheet
xlsx1 = pd.ExcelFile('file1.xlsx')
sheets1 = {sheet_name: xlsx1.parse(sheet_name) for sheet_name in xlsx1.sheet_names}
# 讀取第二個Excel文件的所有Sheet
xlsx2 = pd.ExcelFile('file2.xlsx')
sheets2 = {sheet_name: xlsx2.parse(sheet_name) for sheet_name in xlsx2.sheet_names}

第三步:對比數(shù)據(jù)

對比兩個Excel文件中相同名稱的Sheet。我們可以逐個Sheet進行對比,尋找不一致的數(shù)據(jù)行。


# 創(chuàng)建一個空的字典來存儲對比結果
comparison_results = {}
for sheet_name in sheets1.keys():
    if sheet_name in sheets2:
        # 如果兩個文件都有相同的Sheet,則進行對比
        df1 = sheets1[sheet_name]
        df2 = sheets2[sheet_name]
        # 比較兩個DataFrame
        comparison = df1.merge(df2, how='outer', indicator=True)
        comparison_results[sheet_name] = comparison[comparison['_merge'] != 'both']

第四步:分析差異

上述對比會返回一個新DataFrame,其中包含標記為left_only或right_only的行,表示只在左側或右側數(shù)據(jù)集中存在。此外,還可以通過left和right后綴訪問原始數(shù)據(jù)列。


# 分析差異
for sheet_name, result in comparison_results.items():
    if not result.empty:
        print(f"Differences found in '{sheet_name}':")
        print(result)

第五步:保存對比結果

將對比結果保存到新的Excel文件中,便于后續(xù)分析或報告。


with pd.ExcelWriter('comparison_results.xlsx') as writer:
    for sheet_name, result in comparison_results.items():
        if not result.empty:
            result.to_excel(writer, sheet_name=sheet_name, index=False)

完整代碼示例

下面是將上述步驟整合在一起的完整代碼示例:


import pandas as pd
# 讀取Excel文件
xlsx1 = pd.ExcelFile('file1.xlsx')
xlsx2 = pd.ExcelFile('file2.xlsx')
# 讀取所有Sheet
sheets1 = {sheet_name: xlsx1.parse(sheet_name) for sheet_name in xlsx1.sheet_names}
sheets2 = {sheet_name: xlsx2.parse(sheet_name) for sheet_name in xlsx2.sheet_names}
# 創(chuàng)建一個空的字典來存儲對比結果
comparison_results = {}
# 對比數(shù)據(jù)
for sheet_name in sheets1.keys():
    if sheet_name in sheets2:
        df1 = sheets1[sheet_name]
        df2 = sheets2[sheet_name]
        comparison = df1.merge(df2, how='outer', indicator=True)
        comparison_results[sheet_name] = comparison[comparison['_merge'] != 'both']
# 保存對比結果
with pd.ExcelWriter('comparison_results.xlsx') as writer:
    for sheet_name, result in comparison_results.items():
        if not result.empty:
            result.to_excel(writer, sheet_name=sheet_name, index=False)

通過上述步驟,你可以有效地對比兩個Excel文件中多個Sheet的數(shù)據(jù),找出差異并保存結果。這種方法特別適用于財務審計、數(shù)據(jù)清洗或任何需要跨數(shù)據(jù)集一致性檢查的場景。

希望這篇指南能夠幫助你在Python中處理復雜的Excel數(shù)據(jù)對比任務。

責任編輯:華軒 來源: 測試開發(fā)學習交流
相關推薦

2021-02-27 09:30:16

PythonExcelSheet

2021-01-14 10:24:55

壓縮集合方式

2021-03-10 09:38:44

JeecgBoot 單表數(shù)據(jù)sheet實例

2022-06-17 09:46:51

Chrome 102Chrome瀏覽器

2016-10-25 13:58:36

數(shù)據(jù)圖表化大數(shù)據(jù)

2011-09-20 15:19:15

Python

2020-10-25 08:47:36

Python有序字典

2020-04-17 10:13:51

Python開發(fā)工具

2020-11-13 07:16:09

線程互斥鎖死循環(huán)

2019-08-28 10:00:34

Python測試工具命令

2021-04-06 11:44:44

數(shù)據(jù)平臺數(shù)據(jù)科學數(shù)據(jù)倉庫

2015-11-12 10:32:06

數(shù)據(jù)中心數(shù)據(jù)中心優(yōu)化數(shù)據(jù)中心整合

2010-04-06 18:04:09

Oracle數(shù)據(jù)庫

2010-09-10 15:26:05

SOAP封裝

2009-07-16 10:39:00

SwingUtilit

2022-03-07 13:58:30

JavaScript原始數(shù)據(jù)前端

2011-04-15 10:21:07

Oracle數(shù)據(jù)庫運算

2010-03-18 14:22:46

Python數(shù)據(jù)庫連接

2011-06-29 09:16:40

Python

2022-07-05 15:15:11

Python日期命令
點贊
收藏

51CTO技術棧公眾號