0

I have two dataframe: df1, df2

name     gender
John      1
David     1 
Lilly     0
Young     0
Lisa      0
Wang      1

 name     gender
 Andy      1
 Lilly     1 
 Wang     0
 Mina    0
 John     0
 Claire      1

The two dataframes are both name-gender pair. I am trying to detect same names but assigned different gender in both dataframe(e.g., Lilly in df1 is assigned 0 and in df2 is assigned 1). I want to print a dataframe only shows that.

I used code

df2[df2[df1['name'] == df2['name'] and df2['gender'] != df1['gender']]

This code doesn't return an error but kept running forever. I wonder what did I do wrong. Is there any pandas command that can solve this?

  • [Comparing 2 columns of two Python Pandas dataframes and getting the common rows](https://stackoverflow.com/questions/30291032/comparing-2-columns-of-two-python-pandas-dataframes-and-getting-the-common-rows) – Trenton McKinney Jul 27 '20 at 23:28

1 Answers1

0

merge filter then drop

df2.merge(df1,on='name',suffixes=('','d')).query('gender!=genderd').drop('genderd',1)
   name  gender
0  Lilly       1
1   Wang       0
2   John       0
BENY
  • 317,841
  • 20
  • 164
  • 234