Skip to content Skip to sidebar Skip to footer

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 notsal

Solution 1:

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()

Post a Comment for "Selecting Only Required Keys From A Dictionary Using A Dataframe"