简体   繁体   中英

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

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]

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM