Skip to content Skip to sidebar Skip to footer

Eliminating Duplicates Based On Conditions In Data Frame

This is my data frame: Fruits Person Eat Banana Peter Yes Banana Ashley Yes Strawberry Peter No Strawberry Ashley

Solution 1:

Try this:

df1 = (df[df.Eat.eq('Yes')].sort_values('Person')
                           .drop_duplicates(subset='Fruits', keep='last'))

Out[14]:
       Fruits  Person  Eat
3  Strawberry  Ashley  Yes
7       Grape  Ashley  Yes
0      Banana   Peter  Yes
4      Cherry   Peter  Yes
9        Pear   Peter  Yes

Post a Comment for "Eliminating Duplicates Based On Conditions In Data Frame"