Home > Blockchain >  Is there a function to remove duplicates within a row without removing the entire row using Python?
Is there a function to remove duplicates within a row without removing the entire row using Python?

Time:08-13

enter image description here

Now to delete or replace the duplicate with empty space:

df['Problem2']=df.apply(lambda x:x["Problem2"] if not(x["Problem2"]==x['Problem1']) else " ",axis=1)


df['Problem3']=df.apply(lambda x:x["Problem3"] if not(x["Problem3"]==x['Problem2'] or x["Problem3"]==x['Problem1']) else " ",axis=1)
df

enter image description here

CodePudding user response:

You can try to use the df.duplicated-function for this. This works similar to df.drop_duplicates but returns a boolean series instead of removing the duplicates. You can then index your initial dataframe by this boolean series setting the values to None.

  • Related