'How to change merge result to list and use openpyxl write into column?

  1. I have two different excel to compare...

         Data1.xlsx                                     Data2.xlsx
    |  Name   |  Reg Date  |    |  Name   |   City    |  Reg Date  | Gender | Data1.xlsx |
    |Annie    | 2021-07-01 |    |Alex     | Hong Kong | 2021-07-04 | Male   |            |
    |Billy    | 2021-07-02 |    |Annie    | Hong Kong | 2021-07-01 | Female |            |
    |Cathrine | 2021-07-03 |    |Bob      | Taipei    | 2021-07-02 | Male   |            |
    |David    | 2021-07-04 |    |Lucy     | Tokyo     | 2021-07-01 | Female |            |
    |Eric     | 2021-07-04 |    |David    | London    | 2021-07-04 | Male   |            |
                                |Kate     | New York  | 2021-07-03 | Female |            |
                                |Cathrine | London    | 2021-07-03 | Female |            |
                                |Rose     | Hong Kong | 2021-07-04 | Female |            |
    
  2. I get Name & Reg Date for key to merge and iloc for Data1.xlsx

    import openpyxl as xl
    import pandas as pd
    dt1 = pd.read_excel('Data1.xlsx')
    dt2 = pd.read_excel('Data2.xlsx')
    df_merge = pd.merge(dt2, dt1[['Name', 'Reg Date']], on=['Name', 'Reg Date'], how='left', indicator=True)
    df_merge['Data1.xlsx'] = np.where(df_merge.pop('_merge').eq('both'), 'Y', 'N')
    
       Name      City       Reg Date    Gender  Data1.xlsx 
    0  Alex      Hong Kong  2021-07-04  Male    N            
    1  Annie     Hong Kong  2021-07-01  Female  Y           
    2  Bob       Taipei     2021-07-02  Male    N           
    3  Lucy      Tokyo      2021-07-01  Female  N           
    4  David     London     2021-07-04  Male    Y           
    5  Kate      New York   2021-07-03  Female  N           
    6  Cathrine  London     2021-07-03  Female  Y           
    7  Rose      Hong Kong  2021-07-04  Female  N
    
    merge_loc = df_merge.iloc[:, [4]]
    
       Data1.xlsx 
    0  N            
    1  Y           
    2  N           
    3  N           
    4  Y           
    5  N           
    6  Y           
    7  N
    
  3. I use openpyxl read Data2.xlsx excel file

    wb = xl.Workbook('Data2.xlsx')
    ws = wb.active
    
  4. I want to ask how to change merge_loc to list with hidden index and use openpyxl write list into column E of Data2.xlsx?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source