转码.py 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. import pandas as pd
  2. # 读取两个表
  3. table1 = pd.read_excel(r"D:\guozhong\19、定远县\19、定远县\定远县转码表.xlsx",converters={"样品编号":str}) # 表1
  4. table2 = pd.read_excel(r"D:\guozhong\19、定远县\19、定远县\定远县表层数据统计20241222.xlsx") # 表2
  5. # 确保两表列数据类型一致,转换为字符串
  6. table1["转码后样品编号"] = table1["样品编号"].astype(str)
  7. table1["样品编号"] = table1["原样品编号"].astype(str)
  8. table2["样品编号"] = table2["样品编号"].astype(str)
  9. print(table1)
  10. print(table2)
  11. # 提取表2的样品编号列
  12. sample_ids_table2 = table2["样品编号"].tolist()
  13. print(sample_ids_table2)
  14. # 创建一个列表,用于存储匹配结果
  15. matched_results = []
  16. # 遍历 table2 的样品编号,与 table1 的转码后样品编号匹配
  17. for sample_id in sample_ids_table2:
  18. matches = table1[table1["转码后样品编号"] == sample_id]
  19. for _, row in matches.iterrows():
  20. matched_results.append((sample_id, row["样品编号"]))
  21. # 将结果转为 DataFrame 并保存到 Excel 文件
  22. output_df = pd.DataFrame(matched_results, columns=["表2样品编号", "表1样品编号"])
  23. output_path = r"D:\guozhong\定远表层转码.xlsx"
  24. output_df.to_excel(output_path, index=False)
  25. print(f"匹配完成!结果已保存到 {output_path}")