pandas drop rows that share similar value in other column

Viewed 77

I have DataFrame df = pd.DataFrame({'col1': ["a","b","c","d","e"], 'col2': [1,3,3,2,6]}) that looks like

Input:

 col1 col2
0   a   1
1   b   3
2   c   3
3   d   2
4   e   6

I would like to remove rows from "col1" that share a common value in "col2". The expected output would look something like...

Output:

 col1 col2
0   a   1
3   d   2
4   e   6

What would be the process of doing this?

2 Answers

using this short code should do the trick

df.drop_duplicates(subset=['col2'], keep=False)

Explanation

we use drop_duplicates to (ovbiously) drop the duplicates, and we set the column(s) we want to drop from to be col2 as you requested, In order to drop all occurences (and not keep the first occurence of each duplicate for example) we use keep=False.

This will do the trick


from collections import Counter

df = pd.DataFrame({'col1': ["a","b","c","d","e"], 'col2': [1,3,3,2,6]})

c = Counter(df['col2'])
ls = [k for k,v in c.items() if v==1]

_fltr = df['col2'].isin(ls)

df.loc[_fltr,:]
Related