48 lines
1.7 KiB
Python
48 lines
1.7 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_income = pd.read_excel(file_path, sheet_name="收入", header=1)
|
|
|
|
# 归属本案金额、到账金额、到账日期、缴款人、案号
|
|
# 对方户名、交易时间、金额
|
|
|
|
# 统一日期格式
|
|
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)
|
|
|
|
# # 输出结果
|
|
# print(filtered_df)
|
|
|
|
# output_file_path = "/home/baol/tools/case1_records.xlsx" # 替换为你希望保存的文件路径
|
|
# filtered_df.to_excel(output_file_path, index=False)
|
|
|
|
# print(f"匹配结果已保存到 {output_file_path}")
|
|
|
|
|
|
# 检索出在某一天同时有多笔金额相同的记录
|
|
duplicate_records = merged_df.groupby(["金额", "交易时间"]).filter(lambda x: len(x) > 1)
|
|
|
|
# 将结果保存为新的Excel文件
|
|
output_file_path = "/home/baol/tools/case2_records.xlsx" # 替换为你希望保存的文件路径
|
|
duplicate_records.to_excel(output_file_path, index=False, engine="openpyxl")
|