(根据某一列剔除另一表中出现过的记录)
一、两表结构
从A表中筛选出B表中出现过的key值
二、步骤说明
1、读取表格
aData = pd.read_excel('A.xlsx') # 读取A表
bData = pd.read_excel('B.xlsx') # 读取B表
2、利用merge()函数关联key值,设置indicator=True
aData = pd.merge(aData, bData['key'], on='key', how='left', indicator=True)
得到aData表,如图:
key | properties | _merge |
2021 | 1 | both |
2022 | 0 | left_only |
2023 | 0 | left_only |
2024 | 0 | left_only |
3、筛选仅左表存在的记录行
Data= aData[aData._merge == 'left_only']
key | properties | _merge |
2022 | 0 | left_only |
2023 | 0 | left_only |
2024 | 0 | left_only |
筛选列并保存
Data= Data[['key','properties']]
Data.to_excel('Data.xlsx', index=None)
key | properties |
2022 | 0 |
2023 | 0 |
2024 | 0 |
三、附录
import pandas as pd
aData = pd.read_excel('A.xlsx') # 读取A表
bData = pd.read_excel('B.xlsx') # 读取B表
aData = pd.merge(aData, bData['key'], on='key', how='left', indicator=True)
Data= aData[aData._merge == 'left_only']
Data= Data[['key','properties']]
Data.to_excel('Data.xlsx', index=None)