Skip to content Skip to sidebar Skip to footer

Pandas Dataframe Check Intersection And Fill In A New Dataframe

I have two lists of protein sequences, I have to check every entry's existence in the two lists, say like list A = [1,2,3,4] list B= [3,4,5] ## just an example. The result would

Solution 1:

You can use merge with indicator turned on which creates a _merge column that gives information about whether the value in the join column exists in the left or right or both data frames, and then you can create two indication columns from it:

df1 = pd.DataFrame({'name': A})
df2 = pd.DataFrame({'name': B})

(df1.merge(df2, how='outer', indicator=True)
 .assign(inv6 = lambda x: x._merge != "right_only", 
         inv9 = lambda x: x._merge != "left_only")
 .drop("_merge", 1))

enter image description here

Post a Comment for "Pandas Dataframe Check Intersection And Fill In A New Dataframe"