Juzar Para
Juzar Para

Reputation: 43

Delete duplicate by using python pandas

I want to delete all record with condition

import pandas as pd
import numpy as np

# Create a DataFrame
d = {
    'Name':['Alisa','Bobby','jodha','jack','raghu','Cathrine',
        'Alisa','Bobby','kumar','Alisa','Alex','Cathrine'],
        'Age':[26,24,23,22,23,24,26,24,22,23,24,24],
        'Score':[85,63,55,74,31,77,85,63,42,62,89,77]}
df = pd.DataFrame(d,columns=['Name','Age','Score'])
df

I want to remove all the record of "Alisa" which is duplicate as she is having Score = 85

I have tried below code, but it still displays "Alisa"

df1 = df[df['Score']==85]
df.drop_duplicates(['Name'])

Upvotes: 0

Views: 39

Answers (1)

Mykola Zotko
Mykola Zotko

Reputation: 17834

If you want to drop all duplicates where 'Score' is equal to 85 you can use the following solution:

df1 = df[df['Score'] == 85].drop_duplicates(keep='last')
df.drop(df1.index, inplace=True)

Upvotes: 1

Related Questions