Python 3.x 返回dict和数据帧记录之间的唯一差异

Python 3.x 返回dict和数据帧记录之间的唯一差异,python-3.x,pandas,Python 3.x,Pandas,我有像下面的图像属性数据这样的数据。我想将一个dict值与数据帧中指定的记录列表进行比较,并返回一个dict,其中的列和值是原始dict所独有的 所以在这个例子中,我将“purch”dict与image_id=[16151561]的记录进行比较。我希望返回我的代码: {('Sleeve', 'Long sleeves')} 现在它返回的列和值对于每个记录都是不同的。有人知道我如何过滤最后的dict,只返回一个具有唯一列和值的dict(如上面的示例所示) img\u attr\u df:

我有像下面的图像属性数据这样的数据。我想将一个dict值与数据帧中指定的记录列表进行比较,并返回一个dict,其中的列和值是原始dict所独有的

所以在这个例子中,我将“purch”dict与image_id=[16151561]的记录进行比较。我希望返回我的代码:

{('Sleeve', 'Long sleeves')}
现在它返回的列和值对于每个记录都是不同的。有人知道我如何过滤最后的dict,只返回一个具有唯一列和值的dict(如上面的示例所示)

img\u attr\u df

   image_id Neckline         Sleeve Skin_exposure
0       619  V-shape   Long sleeves  Low exposure
1      1615  V-shape  Short sleeves  Low exposure
2      1561    Round  Short sleeves  Low exposure
purch

   image_id Neckline        Sleeve Skin_exposure
0       619  V-shape  Long sleeves  Low exposure
代码:

输出:

[{('Sleeve', 'Long sleeves')},
 {('Neckline', 'V-shape'), ('Sleeve', 'Long sleeves')}]
期望输出:

{('Sleeve', 'Long sleeves')}

我使用
isin

def diff_attributes(df_na,dataset,To_compare):
    compared=[]
    for i in dataset.columns[1:]:
        if ~dataset[i].isin(df_na.loc[df_na['image_id'].isin(To_compare),i]).any():
            compared.append((i,dataset[i][0]))
    return compared
input_df=df[['image_id','Neckline','Sleeve','Skin_exposure']]
comp_list=[1615,1561]
diff_attributes(input_df,purch,comp_list)
Out[142]: [('Sleeve', 'Longsleeves')]
@用户3476463 yw:-)快乐编码
def diff_attributes(df_na,dataset,To_compare):
    compared=[]
    for i in dataset.columns[1:]:
        if ~dataset[i].isin(df_na.loc[df_na['image_id'].isin(To_compare),i]).any():
            compared.append((i,dataset[i][0]))
    return compared
input_df=df[['image_id','Neckline','Sleeve','Skin_exposure']]
comp_list=[1615,1561]
diff_attributes(input_df,purch,comp_list)
Out[142]: [('Sleeve', 'Longsleeves')]