70 lines
2.6 KiB
Python
70 lines
2.6 KiB
Python
import pandas as pd
|
|
|
|
# 读取Excel文件
|
|
file_path = "/home/baol/tools/数据草稿-案款.xls" # 替换为你的Excel文件路径
|
|
|
|
# 读取“账户收”工作表,只保留指定列
|
|
sheet_account_receipt = pd.read_excel(file_path, sheet_name="账户收")
|
|
sheet_account_receipt = sheet_account_receipt[
|
|
["归属本案金额", "到账金额", "到账日期", "缴款人", "案号"]
|
|
]
|
|
|
|
# 读取“收入”工作表,表头从第二行开始,只保留指定列
|
|
sheet_income = pd.read_excel(file_path, sheet_name="收入", header=1)
|
|
sheet_income = sheet_income[["对方户名", "交易时间", "金额"]]
|
|
|
|
# 统一日期格式
|
|
sheet_account_receipt["到账日期"] = pd.to_datetime(
|
|
sheet_account_receipt["到账日期"]
|
|
).dt.strftime("%Y-%m-%d")
|
|
sheet_income["交易时间"] = pd.to_datetime(sheet_income["交易时间"]).dt.strftime(
|
|
"%Y-%m-%d"
|
|
)
|
|
|
|
# 重命名列以便于合并
|
|
sheet_account_receipt.rename(
|
|
columns={"到账金额": "金额", "到账日期": "交易时间"}, inplace=True
|
|
)
|
|
|
|
# 合并两个数据框
|
|
merged_df = pd.merge(
|
|
sheet_account_receipt, sheet_income, on=["金额", "交易时间"], how="inner"
|
|
)
|
|
|
|
# 排除在某一天同时有多笔金额相同的记录
|
|
filtered_df = merged_df.groupby(["金额", "交易时间"]).filter(lambda x: len(x) == 1)
|
|
|
|
# 检索出在某一天同时有多笔金额相同的记录
|
|
duplicate_records = merged_df.groupby(["金额", "交易时间"]).filter(lambda x: len(x) > 1)
|
|
|
|
# 将结果保存为新的Excel文件
|
|
output_file_path_filtered = "/home/baol/tools/matched_records.xlsx" # 匹配结果
|
|
output_file_path_duplicates = "/home/baol/tools/duplicate_records.xlsx" # 重复记录
|
|
|
|
filtered_df.to_excel(output_file_path_filtered, index=False, engine="openpyxl")
|
|
duplicate_records.to_excel(output_file_path_duplicates, index=False, engine="openpyxl")
|
|
|
|
print(f"匹配结果已保存到 {output_file_path_filtered}")
|
|
print(f"重复记录已保存到 {output_file_path_duplicates}")
|
|
|
|
|
|
# 合并两个数据框,并标记匹配情况
|
|
out_merged_df = pd.merge(
|
|
sheet_account_receipt,
|
|
sheet_income,
|
|
on=['金额', '交易时间'],
|
|
how='outer', # 使用外连接以保留未匹配的记录
|
|
indicator=True # 标记匹配情况
|
|
)
|
|
|
|
# 筛选出未匹配的记录
|
|
unmatched_records = out_merged_df[out_merged_df['_merge'] != 'both']
|
|
|
|
# 删除用于标记匹配情况的列
|
|
unmatched_records.drop(columns=['_merge'], inplace=True)
|
|
|
|
# 将结果保存为新的Excel文件
|
|
output_file_path_unmatched = '/home/baol/tools/unmatched_records.xlsx' # 未匹配记录
|
|
unmatched_records.to_excel(output_file_path_unmatched, index=False, engine='openpyxl')
|
|
|
|
print(f"未匹配的所有记录已保存到 {output_file_path_unmatched}") |