jeudi 24 juin 2021

How to return values in multiple columns based on conditions in another dataframe column python

I have a main dataframe say DF1 with column values, Category, Subcategory, Group, Text. I need to validate if correct category and subcategory is against each group and text columns.

I have another dataframe say DF2 with same 4 columns and the expected category and subcategory for group and strings to be contained in text column.

DF1:

Category Subcategory Group Text
Sweet Cake G6 mandatory for birthdays and weddings
chocolate DairyMilk G1 Packed in blue, has dark and white combo
Chips Lays G2 Multicolor wraps, different flavors and is liked by many
Chips Bingo G3 Triangle in shape and has multiple flavors
chocolate DairyMilk G5 Blue wrapper

DF2:

Category Subcategory Group Text
Sweet Cake G6,G8,G7
chocolate DairyMilk G1,G10,G5 blue, dark and white
Chips Bingo G3,G4 Triangle, flavours
Chips Lays G 2 Multicolor , flavors

I need to add predicted category and subcategory in DF1 based on DF2 values. Example if Condition1:

DF1['Group'] is G6 or G8 or G7, DF1['predictedCategory']=='Sweet' and DF1['predictedSubcategory'] == 'Cake'

Condition2:

DF1['Group'] is G1 or G10 or G5 and DF1['Text'] contains blue or dark and white, DF1['predictedCategory']=='chocolate' and DF1['predictedSubcategory'] == 'DairyMilk'.

Would need a precise approach which would also save time in running the code. I tried writing if else statement with multiple conditions but it returns empty in predicted columns. Would appreciate any leads.

Aucun commentaire:

Enregistrer un commentaire