使用数据框减去字典中列表的值 [英] subtracting values of a list in a dictionary using a dataframe

查看:59
本文介绍了使用数据框减去字典中列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个数据框,其中包含用户购买的产品.

I have a dataframe below, with the products purchased by users.

数据集:

user    age maritalstatus   product
A   Young   married 111
B   young   married 222
C   young   Single  111
D   old single  222
E   old married 111
F   teen    married 222
G   teen    married 555
H   adult   single  444
I   adult   single  333

字典:

{A:[111,222], B:[111,222], C:[111], D:[222], G:[222,555], X:[222,444] } 

预期输出:

{A:[222], B:[111], C:[], D:[], G:[222], X:[222,444] }

字典应查看数据框并删除用户已经购买的产品.

The dictionary should look into the dataframe and remove the products already purchased by the users.

推荐答案

您可以使用字典理解:

{k:[e for e in v if e not in df.loc[df.user.eq(k), 'product'].tolist()] for k,v in d.items()}
Out[292]: {'A': [222], 'B': [111], 'C': [], 'D': [], 'G': [222], 'X': [222, 444]}

稍微冗长的解决方案,以便于理解:

A slightly more verbose solution for easier understanding:

首先构建用户产品字典:

First to build a user-product dict:

user_prod = df.groupby('user')['product'].apply(list).to_dict()
{'A': [111],
 'B': [222],
 'C': [111],
 'D': [222],
 'E': [111],
 'F': [222],
 'G': [555],
 'H': [444],
 'I': [333]}

然后,使用dict理解删除user_prod dict中的元素.

Then, use a dict comprehension to remove elements which are in the user_prod dict.

{k:[e for e in v if e not in user_prod.get(k,[])] for k,v in d.items()}
Out[319]: {'A': [222], 'B': [111], 'C': [], 'D': [], 'G': [222], 'X': [222, 444]}

使用user_prod.get是必要的,因为用户可能不存在,并且.get通过返回一个空列表来避免出现异常.

The use of user_prod.get is necessary because the user may not exist and .get will avoid an exception by returning an empty list.

这篇关于使用数据框减去字典中列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆