Get rows based on individual values ​​from column 2

I am new to pandas, tried to find this on google, but still no luck. How can I get rows for individual values ​​in column2?

For example, I have a dataframe edge:

>>> df
COL1   COL2
a.com  22
b.com  45
c.com  34
e.com  45
f.com  56
g.com  22
h.com  45

I want to get strings based on unique values ​​in COL2

>>> df
COL1  COL2
a.com 22
b.com 45
c.com 34
f.com 56

So how can I get this? I would really appreciate if anyone could be of any help.

+6
source share
1 answer

Use drop_duplicateswith a column COL2to check for duplicates:

df = df.drop_duplicates('COL2')
#same as
#df = df.drop_duplicates('COL2', keep='first')
print (df)
    COL1  COL2
0  a.com    22
1  b.com    45
2  c.com    34
4  f.com    56

You can also save only the latest values:

df = df.drop_duplicates('COL2', keep='last')
print (df)
    COL1  COL2
2  c.com    34
4  f.com    56
5  g.com    22
6  h.com    45

Or delete all duplicates:

df = df.drop_duplicates('COL2', keep=False)
print (df)
    COL1  COL2
2  c.com    34
4  f.com    56
+7
source

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


All Articles