remove a row from a dataframe if any row value is in another dataframe , with dataframes having multiple columns

aryan singh :

I hava two data frames :

df1 = {0:[1,2,3,4,5,6,7,11],1:[100,20,7]}

df2 = {0:[100,4,6,7],1:[1,3,4,7]}

i have to remove rows from df1 that occurs in any row of df2

result dataframe

df3 = [2,5,11,20]
jezrael :

You can flatten values by np.ravel and get difference by np.setdiff1d:

df1 = pd.DataFrame({0:[1,2,3,4,5,6,7,11],1:[100,20,7,1,2,3,4,5]})
df2 = pd.DataFrame({0:[100,4,6,7],1:[1,3,4,7]})

L = np.setdiff1d(np.ravel(df1), np.ravel(df2)).tolist()
print (L)
[2, 5, 11, 20]

Or difference of sets:

L = list(set(df1.stack()) - set(df2.stack()))
print (L)
[2, 11, 20, 5]

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=386399&siteId=1