user10718437
user10718437

Reputation:

Compare two Dateframes

Problem: I have 2 Dataframes:

Name   B     Worker     B
 A4  True     A4       True
 A5  True AND A6       False     
 A6  True     C4       False
 A7  False    C7       True

I want to give out the "Name" where Df1.B == True and Df2.B == False

Upvotes: 3

Views: 51

Answers (3)

edesz
edesz

Reputation: 12406

You could try this

Generate data

data1 = [['Name','B'],['A4',True],['A5',True],['A6',True],['A7',False]]
data2 = [['Name','B'],['A4',True],['A6',False],['C4',False],['C7',True]]
df1 = pd.DataFrame(data1[1:],columns=data1[0])
df2 = pd.DataFrame(data2[1:],columns=data2[0])

print(df1)
  Name      B
0   A4   True
1   A5   True
2   A6   True
3   A7  False

print(df2)
  Name      B
0   A4   True
1   A6  False
2   C4  False
3   C7   True

Filter

df1_filtered = df1.loc[(df1.B) & (~df2.B)]
df2_filtered = df2.loc[(df1.B) & (~df2.B)]

print(df1_filtered['Name'])
1    A5
2    A6
Name: Name, dtype: object

print(df2_filtered['Name'])
1    A6
2    C4
Name: Name, dtype: object

NOTE:

  1. If you want the output from df1 then use df1_filtered. If you require output from df1, then use df2_filtered.

Upvotes: 0

BENY
BENY

Reputation: 323376

Check with isin

df1.loc[(df1.B)&(~df1.name.isin(df2.Worker)),'name']

Update

df1.loc[(df1.B)&(~df2.B),'name']

Upvotes: 1

heena bawa
heena bawa

Reputation: 828

You can use:

df1 = pd.DataFrame({'Name': ['A4', 'A5', 'A6', 'A7'], 'B': [True, True, True, False]})
df2 = pd.DataFrame({'Worker': ['A4', 'A6', 'C4', 'C7'], 'B': [True, False, False, True]})
df1[(df1['B']==True) & (df2['B']==False)]['Name']

Output:

1    A5
2    A6

Upvotes: 0

Related Questions