如何用Python合并多张图像对象等价映射表格?
用Python合并多组两列等价表格的问题
我需要用Python合并n个仅含两列的表格,目前以table12、table13、table23这3个表格进行测试。
背景
我拥有若干图像文件,每个图像中的对象标记为'A'、'B'、'C'等。对比两张图像会生成等价表格,用于说明图像1中的对象与图像2中的哪个对象为同一类(例如table12的第一行表示图像1的'A'与图像2的'P'为同一对象),部分对象可能不存在等价对象。
需求
希望利用所有表格生成一个合并表格,最终表格的列数等于图像数量,同一行中可查看所有互为等价的对象。
尝试的方法及问题
我尝试使用pandas的merge方法(先合并前两个表格,再将合并结果与第三个表格合并),但得到的结果不符合预期。测试代码如下:
import pandas as pd import numpy as np table12 = pd.DataFrame({ 'img1': ['A', 'B', 'C', 'D', np.NaN, np.NaN], 'img2': ['P', np.NaN, 'Q', np.NaN, 'R', 'S'] }) table13 = pd.DataFrame({ 'img1': ['A', 'B', 'C', 'D', np.NaN, np.NaN], 'img3': [np.NaN, 'X', np.NaN, 'Y', 'Z', 'W'] }) table23 = pd.DataFrame({ 'img2': ['P', np.NaN, 'Q', np.NaN, 'R', np.NaN, 'S'], 'img3': [np.NaN, 'X', np.NaN, 'Y', 'Z', 'W', np.NaN] }) merged_12_13 = pd.merge(table12, table13, on='img1', how='outer') final_output = pd.merge(merged_12_13, table23, on=['img2', 'img3'], how='outer')
内容的提问来源于stack exchange,提问作者Ayush Singh
相关产品推荐
相关产品推荐

