user10724844
user10724844

Reputation:

compare two columns and set up conditions

I have two data frames

df1 

    name gender
    John 1
    Mina 1
    Cici 0
    Dean 1
    Lily 0

df2

    n    g
    King 1
    Cici 1
    Cici 1
    Dean 0
    Juli 0

For the two dataframe, I want to return all the rows where

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

Is there any pandas command could solve this?

Upvotes: 0

Views: 30

Answers (3)

Joshua Johns
Joshua Johns

Reputation: 373

df1['name'] == df2['n'] and (df1['gender'] == True)

Upvotes: 0

Javad Rezaei
Javad Rezaei

Reputation: 1110

Following expression will give you boolean result for each row, then if you need those rows separetely, you can use df1[idx]

idx = (df1['name'] == df2['n']) & (df1['gender'] != df2['g'])

Upvotes: 0

BENY
BENY

Reputation: 323236

Yes we do merge

out=df1.merge(df2,left_on='name',right_on='n').query('gender!=g')

Upvotes: 1

Related Questions