arkadiy
arkadiy

Reputation: 766

How to drop Duplicates for each unique row value in Pandas?

I have the following dataframe:

df = pd.DataFrame({

    'ID': [42, 42, 42, 43, 43, 43,58, 58, 58],
    'Thing': ['cup', 'cup', 'plate', 'plate', 'plate', 'plate', 'cup', 'cup', 'plate']
    
})

df
    ID  Thing
0   42  cup
1   42  cup
2   42  plate
3   43  plate
4   43  plate
5   43  plate
6   58  cup
7   58  cup
8   58  plate

I want to drop duplicates from the "Thing" column, but only for each unique ID. I want the result to look like this:

    ID  Thing
0   42  cup
2   42  plate
6   58  cup
8   58  plate

I tried this:

for id in df['ID'].unique():
    df= df.drop_duplicates(subset=['Thing'], keep='first')

But the result looks like this:

    ID  Thing
0   42  cup
2   42  plate

Does anyone know what is the best way to accomplish this in Pandas?

Upvotes: 0

Views: 145

Answers (1)

rhug123
rhug123

Reputation: 8780

Try:

df = df.drop_duplicates(subset = ['ID','Thing'])

Upvotes: 4

Related Questions