Zee
Zee

Reputation: 91

Python groupby and delete certain rows only

A. B. C. 102 12/2019 looking 102 01/2020 won 102 02/2020 archived 102 03/2020 deleted 101 12/2019 looking 101 01/2020 won 101 02/2020 won 101 03/2020 won

I want to delete all reiterations (records/rows) when a certain A goes to won but ONLY if it stays at won (A=101). But if it goes from won to something else then I want it in the data.

result: A. B. C. 102 12/2019 looking 102 01/2020 won 102 02/2020 archived 102 03/2020 deleted 101 12/2019 looking 101 01/2020 won

Upvotes: 1

Views: 58

Answers (1)

jezrael
jezrael

Reputation: 862581

Use boolean indexing with chained conditions with Series.eq and DataFrame.duplicated with inverse mask by ~:

df = df[~(df.duplicated(['A','C']) & df['C'].eq('won'))]
print (df)
     A        B         C
0  102  12/2019   looking
1  102  01/2020       won
2  102  02/2020  archived
3  102  03/2020   deleted
4  101  12/2019   looking
5  101  01/2020       won

Upvotes: 1

Related Questions