Drop Duplicate Values in a Pandas DataFrame Column

Python

 1|  # Will drop any rows that contain duplicate values in the Name column 
 2|  # but will keep the first row that contains a duplicated value
 3|  df.drop_duplicates(subset=['Name'], inplace=True)
 4|  
 5|  # Will drop any rows that contain duplicate values in the Name column 
 6|  # but will keep the last row that contains a duplicated value
 7|  df.drop_duplicates(subset=['Name'], keep='last', inplace=True)
 8|  
 9|  # Will drop any rows that contain duplicate values in the Name column
10|  df.drop_duplicates(subset=['Name'], keep=False, inplace=True)
Did you find this snippet useful?

Sign up for free to to add this to your code library