giupardeb
giupardeb

Reputation: 841

How can I select the rows on dataframe where specific column are different from each other

I have this example of pandas dataframe:

C1 C2
ciao type1
anna type1
anna type2

I would run a query on this dataframe to get all rows that are different on the column C2, but are equal on the column C1. In this specific example, I would like to get this table:

C1 C2
anna type1
anna type2

Is it possible? THANKS!

Upvotes: 0

Views: 39

Answers (1)

BENY
BENY

Reputation: 323276

You can try with groupby and nunique

out = df.groupby('C1').filter(lambda x : any((len(x)>1) & (len(x) == x.nunique())))
Out[167]: 
     C1     C2
1  anna  type1
2  anna  type2

Upvotes: 1

Related Questions