NineWasps
NineWasps

Reputation: 2273

Pandas: delete string with condition

I have df

ID     url     code
111    vk.com   1
111    twitter.com   1
222    facebook.com   1
222    vk.com    1
222    avito.ru   3

Desire output:

ID     url     code
111    vk.com   1
222    facebook.com   1
222    avito.ru     3

I need to delete string, if previous code is equal to this string and ID is equal to this string.

Upvotes: 0

Views: 72

Answers (1)

DocZerø
DocZerø

Reputation: 8567

You can use drop_duplicates() and specify a subset of columns to use.

df.drop_duplicates(['ID', 'code'], keep='first')

This will only consider the ID and code column and will keep the first occurrence, removing the other duplicates.

Upvotes: 2

Related Questions