python pandas remove duplicate rows code example
Example 1: drop duplicates pandas first column
import pandas as pd
data = pd.read_csv("employees.csv")
data.sort_values("First Name", inplace = True)
data.drop_duplicates(subset ="First Name",keep = False, inplace = True)
print(data)
Example 2: remove duplicate row in df
df = df.drop_duplicates()
Example 3: pandas remove repeated index
idx = pd.Index(['lama', 'cow', 'lama', 'beetle', 'lama', 'hippo'])
idx.drop_duplicates(keep='first')
Index(['lama', 'cow', 'beetle', 'hippo'], dtype='object')
idx.drop_duplicates(keep='last')
Index(['cow', 'beetle','lamb', 'hippo'], dtype='object')
idx.drop_duplicates(keep='False')
Index(['cow', 'beetle','hippo'], dtype='object')