'How to use Openpyxl update cell value in excel after compare using Python?
I have two different excel to compare...
Data1.xlsx Data2.xlsx | Name | City | Reg Date | Gender | Check In | Check In Date | | Name | Reg Date | |Alex | Hong Kong | 2021-06-30 | Male | Y | 2021-06-30 | |Annie | 2021-07-01 | |Annie | Hong Kong | 2021-07-01 | Female | | | |Billy | 2021-07-02 | |Bob | Taipei | 2021-06-28 | Male | Y | 2021-06-28 | |Cathrine | 2021-07-03 | |Lucy | Tokyo | 2021-06-28 | Female | Y | 2021-06-28 | |David | 2021-07-04 | |David | London | 2021-07-04 | Male | | | |Eric | 2021-07-04 | |Kate | New York | 2021-07-03 | Female | | | |Cathrine | London | 2021-07-03 | Female | | | |Rose | Hong Kong | 2021-07-04 | Female | | |I get
Name&Reg Datefor key to mergeimport openpyxl as xl import pandas as pd import numpy as np dt1 = pd.read_excel('Data1.xlsx') dt2 = pd.read_excel('Data2.xlsx') df_merge = pd.merge(dt1, dt2[['Name', 'Reg Date']], on=['Name', 'Reg Date'], how='left', indicator=True) Name City Reg Date Gender Check In Check In Date _merge 0 Alex Hong Kong 2021-06-30 Male Y 2021-06-30 left_only 1 Annie Hong Kong 2021-07-01 Female NaN NaN both 2 Bob Taipei 2021-06-28 Male Y 2021-06-28 left_only 3 Lucy Tokyo 2021-06-28 Female Y 2021-06-28 left_only 4 David London 2021-07-04 Male NaN NaN both 5 Kate New York 2021-07-03 Female NaN NaN left_only 6 Cathrine London 2021-07-03 Female NaN NaN both 7 Rose Hong Kong 2021-07-04 Female NaN NaN left_onlyQ1: Is it possible to use
openpyxlto updateCheck In& copyReg DatetoCheck In Datebase ondf_mergevalue isboth?wb = xl.load_workbook('Data1.xlsx') ws = wb.active for row, value in enumerate(df_merge, start=2):Below is my expected results. Please anyone who can teach me?
Updated Data1.xlsx | Name | City | Reg Date | Gender | Check In | Check In Date | |Alex | Hong Kong | 2021-06-30 | Male | Y | 2021-06-30 | |Annie | Hong Kong | 2021-07-01 | Female | Y | 2021-07-01 | |Bob | Taipei | 2021-06-28 | Male | Y | 2021-06-28 | |Lucy | Tokyo | 2021-06-28 | Female | Y | 2021-06-28 | |David | London | 2021-07-04 | Male | Y | 2021-07-04 | |Kate | New York | 2021-07-03 | Female | | | |Cathrine | London | 2021-07-03 | Female | Y | 2021-07-03 | |Rose | Hong Kong | 2021-07-04 | Female | | |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
