2

I have the following table with fictive data:

enter image description here

I want to remove any duplicate rows and keep only the row which contains a positive value in "Won Turnover". Hence, the two rows marked with red should be removed in this case

Moreover, if there are duplicate rows with only Lost Turnover, then the row with the highest turnover should be kept (The bottom two rows).

2 Answers 2

3

Maybe this can do it:

df.sort_values(['Won Turnover', 'Lost Turnover'], ascending=False).drop_duplicates('Supplier')
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't account for the fact that there may not be any Won Turnover and yes multiple Lost Turnover.
@FrancoPiccolo I think this edit should cover all the cases.
Thank you for your answer. This solution gave me the right answer. However, I had to replace 'Supplier' with 'Matching Key' in the drop_duplicates statement
0

First test Won Turnover if only missing value per groups with GroupBy.all and test only max value per Lost Turnover. Chain by & for bitwise AND and add new condition for return all not missing rows per Won Turnover with | for bitwise OR:

m1 = (df.assign(new = df['Won Turnover'].isna())
        .groupby(['Date','Supplier','Customer'])['new'].transform('all'))
m2 = (df.groupby(['Date','Supplier','Customer'])['Lost Turnover'].transform('max')
        .eq(df['Lost Turnover']))
df = df[(m1 & m2) | df['Won Turnover'].notna()]
print (df)

         Date Supplier   Customer  Won Turnover  Lost Turnover
1  25.06.2019     Nike      Pepsi       25000.0            NaN
2  25.06.2019     Nike  McDonalds       10000.0            NaN
3  25.06.2019   Adidas  Coca Cola       12000.0            NaN
5  25.06.2019   Adidas  McDonalds       35000.0            NaN
6  25.06.2019   Adidas      Pepsi           NaN        15000.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.