Everyone, I have some problems for to leaning program by myself
- I have two different excel to compare...
Data1.xlsx
| Name | Reg Date |
|Annie | 2021-07-01 |
|Billy | 2021-07-02 |
|Cathrine | 2021-07-03 |
|David | 2021-07-04 |
|Eric | 2021-07-04 |
Data2.xlsx
| Name | City | Reg Date | Gender | Data1.xlsx |
|Alex | Hong Kong | 2021-07-04 | Male | |
|Annie | Hong Kong | 2021-07-01 | Female | |
|Bob | Taipei | 2021-07-02 | Male | |
|Lucy | Tokyo | 2021-07-01 | Female | |
|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 | |
I get 'Name' & 'Reg Date' for key to merge
import pandas as pd dt1 = pd.read_excel('Data1.xlsx') dt2 = pd.read_excel('Data2.xlsx') df_merge = pd.merge(dt1.iloc[:, [0, 1]], dt2.iloc[:, [0, 2]], on=['Name', 'Reg Date'], how='outer', indicator=True) i = 0 rows_to_color = [] for a in df_merge.iloc[:, [2]].values: if a == 'both': rows_to_color.append(i) i += 1 | Name | Reg Date | _merge | |Alex | 2021-07-04 | right_only | |Annie | 2021-07-01 | both | |Billy | 2021-07-02 | left_only | |Bob | 2021-07-02 | right_only | |Lucy | 2021-07-01 | right_only | |David | 2021-07-04 | both | |Eric | 2021-07-04 | left_only | |Kate | 2021-07-03 | right_only | |Cathrine | 2021-07-03 | both | |Rose | 2021-07-04 | right_only |
I try to coding to highlight 'left_only' & 'right_only' for 'Data2.xlsx', but not work.
def bg_color(col): color = '#ffffff' return 'background-color: %s' % color if i in rows_to_color: for i, x in col.iteritems(): styled = df_merge.style.apply(bg_color)
I have no idea how to make highlight unmatch row and mark 'Y/N' in 'Data2.xlsx', below pic is my expected result. would you mind to teach me how to coding?