pylearner
pylearner

Reputation: 1460

selecting only required keys from a dictionary using a dataframe

I have a data frame with products and their status like below

DataFrame:

products    status
11  sale
22  sale
33  notsale
44  notsale
55  notsale
66  removed
77  removed
88  notsale
99  sale
222 sale
333 removed
444 removed
555 notsale

I also have a users data as a dictionary with a user and the list of products they are interested in.

{1: [11,22,33,555,33], 2:[33,66,77,88,99],3:[11,88,99,222,333,555],4:[333,33,444,44],5:[333,444,22,33,44,55,66]}

what I need to do is, remove the products with status as removed as well as duplicates from the users interest in the above dictionary.

Expected output:

{1: [11,22,33,555,], 2: [33, 88,99], 3:[11,88,99,222,555], 4: [33, 44], 5: [22, 33,44,55]}

Upvotes: 1

Views: 44

Answers (1)

jezrael
jezrael

Reputation: 863246

First filter by boolean indexing values with removed and then in dict comprehension convert values to set for unique values and then remove values of a:

a = df.loc[df['status'] == 'removed', 'products'].tolist()
print (a)
[66, 77, 333, 444]

d = {1: [11,22,33,555,33], 2:[33,66,77,88,99], 
     3:[11,88,99,222,333,555], 4:[333,33,444,44],5:[333,444,22,33,44,55,66]}

d1 = {k: list(set(v)-set(a)) for k, v in d.items()}
print (d1)
{1: [33, 11, 22, 555], 2: [88, 33, 99], 
 3: [11, 555, 99, 222, 88], 4: [33, 44], 5: [33, 44, 22, 55]}

EDIT:

For filter by multiple keywors use isin:

a = df.loc[df['status'].isin(['removed', 'notsale']), 'products'].tolist()

Upvotes: 2

Related Questions