Remove rows from a data frame based on the value of another data frame

Viewed 30

I would like to know how can I do this,

I have a dataframe like this one:

df1

Tweet           Other columns  ....
-------         -----------    -----    
"Hello world"      ...         ...
"Good morning"     ...         ...
"Hi"               ...         ...

And another one like this one:

df2

Tweet           Other columns    ....
-------         -----------     
"Yes"              ...           ...
"Test"             ...           ...
"Hello world"      ...           ...

I want to remove the rows from df2 that appear in the column Tweet of df1.

And have something like this:

df2

Tweet           Other columns  ....
-------         -----------    -----    
"Yes"               ...         ...
"Test"              ...         ...
"Hi"                ...         ...

(I removed the row with the Tweet value = "Hello world") because this text appear in the column Tweet of the first data frame.

1 Answers

Maybe try np.where:

df2['Tweet'] = np.where(df2['Tweet'].isin(df1['Tweet']), df1['Tweet'], df2['Tweet'])

Or shorter use loc:

df2.loc[df2['Tweet'].isin(df1['Tweet']), 'Tweet'] = df1['Tweet']
Related