辅助处理excel表格,按指定列寻找重复行
此技能只读, 没有副作用是一项面向实际任务的技能,主要用于遵循 [ [excel- safe- work flow]] Scout 分析双步方法;@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ excel- safe- workflow] Excel Find 重复( 只读) /。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;
涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
This skill is read-only, no side effects. Follows [[excel-safe-workflow]] Scout→Analyze two-step approach. 本技能只读不写,安全无副作用。遵循 [[excel-safe-workflow]] 勘察→分析两步。
| Element / 要素 | Common Phrasing / 常见表述 | Default / 默认值 |
|---|---|---|
| Key Column(s) / 关键列 | "By patent number" / "Column E" / "按专利号查""E列" | Must be explicit / 必须明确 |
| Keep Strategy / 保留策略 | "Keep first" / "Keep latest" / "保留第一个""保留最新的" | Keep first occurrence / 保留首次出现 |
| Output Format / 输出格式 | Directly return row number list / 直接返回行号列表 | Excel row numbers / Excel 行号 |
import pandas as pd
FILE = 'target.xlsx' / FILE = '目标文件.xlsx'
KEY_COL = 'Column Name / 列名' # Key column name / 关键列名
KEEP = 'first' # 'first'=keep first occurrence / 保留首次 / 'last'=keep last / 保留末次
# pandas efficient read (C engine, seconds-level) / pandas 高效读取(C引擎,秒级)
df = pd.read_excel(FILE)
total = len(df)
mask = df[KEY_COL].duplicated(keep=KEEP)
dup_indices = df.index[mask].tolist()
dup_excel_rows = [i + 2 for i in dup_indices] # +2: pandas 0-index → Excel row number (row 1=header) / pandas 0-index → Excel行号(第1行=表头)
print(f'Total rows: {total} / 总行数: {total}')
print(f'Unique values: {total - len(dup_excel_rows)} / 唯一值: {total - len(dup_excel_rows)}')
print(f'Duplicate rows: {len(dup_excel_rows)} ({len(dup_excel_rows)/total*100:.1f}%) / 重复行: {len(dup_excel_rows)}')
print(f'Row range: {min(dup_excel_rows)} ~ {max(dup_excel_rows)}' if dup_excel_rows else 'No duplicates / 无重复')
KEY_COLS = ['Col1 / 列名1', 'Col2 / 列名2'] # Multi-column joint / 多列联合 mask = df.duplicated(subset=KEY_COLS, keep=KEEP)
if not dup_excel_rows:
print('✅ No duplicates / 无重复数据')
else:
print(f'nDuplicate row number list (total {len(dup_excel_rows)} rows) / 重复行号列表(共{len(dup_excel_rows)}行):')
print(dup_excel_rows[:20]) # First 20 / 前20个
if len(dup_excel_rows) > 20:
print(f'... and {len(dup_excel_rows)-20} more rows / 还有{len(dup_excel_rows)-20}行')
# Pass to excel-delete for use / 传递给 excel-delete 使用
# Format: [row number list], sort descending then delete_rows one by one / 格式: [行号列表], 从大到小排序后逐个 delete_rows
Find-duplicates output directly feeds into delete input: / 查重输出直接作为删除输入:
excel-find-duplicates → [2, 5, 8, 3, 12, ...] → excel-delete delete bottom-to-top / 从下到上删除
Delete-side code / 删除侧代码:
# Receive find-duplicates results / 接收查重结果
dup_rows = [2, 5, 8, 3, 12, ...] # From excel-find-duplicates / 来自 excel-find-duplicates
# Delete bottom-to-top (critical! avoids row number shifting) / 从下到上删除(关键!避免行号偏移)
for row in sorted(dup_rows, reverse=True):
ws.delete_rows(row)
相关专题
热门下载
相关下载
精品课程
共162课时 | 45.6万人学习
共15课时 | 1.9万人学习
共28课时 | 3.6万人学习
最新文章