3

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

3 Answers 3

1

Check with isin

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

Update

df1.loc[(df1.B)&(~df2.B),'name'] 
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry. I did a mistake. I changed my Question!
@JDog so you want A5 and A6 as output ?
Yes A5 an A6 as output (every Name where df1.B == true and df2.B == False )
0

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 

Comments

0

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.

Comments