Subtracting Values Of A List In A Dictionary Using A Dataframe
I have a dataframe below, with the products purchased by users. DataSet : user age maritalstatus product A Young married 111 B young married 222 C young Single 11
Solution 1:
You can use a dict comprehension:
{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]}
Then, use a dict comprehension to remove elements which are in the user_prod dict.
{k:[e forein v if e not in user_prod.get(k,[])] fork,v in d.items()}
Out[319]: {'A': [222], 'B': [111], 'C': [], 'D': [], 'G': [222], 'X': [222, 444]}
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.
Solution 2:
Here is one intuitive way to implement your logic. You can optimize via sets and comprehensions, but for reasonable size datasets the below method should be adequate.
products = df.groupby('user')['product'].apply(list)
d = {'A':[111,222], 'B':[111,222], 'C':[111], 'D':[222], 'G':[222,555], 'X':[222,444] }
fork, v in d.items():
p = products.get(k)
if p:
foriin p:
d[k].remove(i)
# {'A': [222], 'B': [111], 'C': [], 'D': [], 'G': [222], 'X': [222, 444]}
Solution 3:
product user01101211211231134214
new_purchase = frame.set_index('user')['product'].to_dict()
{10:1, 11:2, 12:1, 13:1, 14:2}
{10: [2, 1], 11: [2], 12: [], 13: [22], 14: [1]}
result = {}
fork, v in prev_purchase.items():
result[k] = [item foritemin v if item not in [new_purchase[k]]]
{10: [2], 11: [], 12: [], 13: [22], 14: [1]}
Post a Comment for "Subtracting Values Of A List In A Dictionary Using A Dataframe"