I discovered a categorization error in a dataset I was exploring.
The dataframe looks like this:
df1 = pd.DataFrame({
'product': ['ABRICOTS', 'ABRICOTS', 'ABRICOTS', 'ABRICOTS', 'AILS','AILS', 'ANANAS', 'ANANAS', 'ANANAS','ANANAS','ANANAS','ANANAS','ANANAS'],
'family': ['Fruit','Fruit','Fruit', 'Vegetable', 'Vegetable','Vegetable', 'Fruit', 'Fruit','Fruit','Fruit','Cereal','Cereal','Animal Product']
})
Each product is supposed to belong to only 1 family category, but using the formula below revealed that I have products with different categories:
df1.groupby(['product','family']).size().rename('Freq').reset_index()
Now what I would like to do, is to use a formula to change for each product the family(ies) with the lowest frequency number to the one with the highest in my original dataframe. The expected result would look like this:




