如何将合并结果更改为列表并使用 openpyxl 写入列?
How to change merge result to list and use openpyxl write into column?
我有两个不同的excel要比较...
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 | |
我得到 Name
& Reg Date
用于合并键,iloc
用于 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
我用openpyxl
读取Data2.xlsx
excel文件
wb = xl.Workbook('Data2.xlsx')
ws = wb.active
我想请问如何将merge_loc
改为带隐藏索引的列表,并使用openpyxl
将列表写入Data2.xlsx
的第E
列?
您需要遍历数组来写入值:
# compute the data
to_write = np.where(df_merge['_merge'].eq('both'), 'Y', 'N')
# array(['N', 'Y', 'N', 'N', 'Y', 'N', 'Y', 'N'], dtype='<U1')
# write the data
import openpyxl as xl
wb = xl.load_workbook('Data2.xlsx')
ws = wb.active
for row, value in enumerate(to_write, start=2):
ws[f'E{row}'].value = value
wb.save('Data2.xlsx')
我有两个不同的excel要比较...
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 | |
我得到
Name
&Reg Date
用于合并键,iloc
用于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
我用
openpyxl
读取Data2.xlsx
excel文件wb = xl.Workbook('Data2.xlsx') ws = wb.active
我想请问如何将
merge_loc
改为带隐藏索引的列表,并使用openpyxl
将列表写入Data2.xlsx
的第E
列?
您需要遍历数组来写入值:
# compute the data
to_write = np.where(df_merge['_merge'].eq('both'), 'Y', 'N')
# array(['N', 'Y', 'N', 'N', 'Y', 'N', 'Y', 'N'], dtype='<U1')
# write the data
import openpyxl as xl
wb = xl.load_workbook('Data2.xlsx')
ws = wb.active
for row, value in enumerate(to_write, start=2):
ws[f'E{row}'].value = value
wb.save('Data2.xlsx')