简体   繁体   中英

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?

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM