我要删除所有有条件的记录
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
我要删除“ Alisa”的所有记录,因为她的成绩= 85,所以重复了
我尝试了下面的代码,但是它仍然显示“ Alisa”
df1 = df[df['Score']==85]
df.drop_duplicates(['Name'])
答案 0 :(得分:1)
如果要删除“分数”等于85的所有重复项,可以使用以下解决方案:
df1 = df[df['Score'] == 85].drop_duplicates(keep='last')
df.drop(df1.index, inplace=True)