Df.unique () on entire column based DataFrame

I have a DataFrame dffilled with rows and columns where there are duplicate identifiers:

Index   Id   Type
0       a1   A
1       a2   A
2       b1   B
3       b3   B
4       a1   A
...

When i use:

uniqueId = df["Id"].unique() 

I get a list of unique identifiers.

How can I apply this filtering to the entire DataFrame to keep the structure, but duplicates (based on "Id") are removed?

+4
source share
1 answer

It seems you need DataFrame.drop_duplicateswith a parameter subsetthat indicates where the test duplicates are:

#keep first duplicate value
df = df.drop_duplicates(subset=['Id'])
print (df)
       Id Type
Index         
0      a1    A
1      a2    A
2      b1    B
3      b3    B

#keep last duplicate value
df = df.drop_duplicates(subset=['Id'], keep='last')
print (df)
       Id Type
Index         
1      a2    A
2      b1    B
3      b3    B
4      a1    A

#remove all duplicate values
df = df.drop_duplicates(subset=['Id'], keep=False)
print (df)
       Id Type
Index         
1      a2    A
2      b1    B
3      b3    B
+3
source

Source: https://habr.com/ru/post/1673888/


All Articles