Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete rows that exist in another data frame? [duplicate]

I have the two following data frames (example):

df1:

name    profile    type    strand A       4.5        1       + B       3.2        1       + C       5.5        1       + D       14.0       1       - E       45.1       1       - F       32.8       1       - G       19.9       1       + 

df2:

name A B C G 

I would like to delete the rows in df1 for which df1$name = df2$name to get the following:

Output:

name    profile    type    strand D       14.0       1       - E       45.1       1       - F       32.8       1       - 

If anyone could tell me which piece of code to use it would be a lot of help, seemed simple at first but I've been messing it up since yesterday.

like image 972
biohazard Avatar asked Jun 27 '13 08:06

biohazard


People also ask

How do you delete a row from a Dataframe that exists in another Dataframe?

To remove rows from a data frame that exists in another data frame, we can use subsetting with single square brackets. This removal will help us to find the unique rows in the data frame based on the column of another data frame.

How do I remove rows from two conditions in R?

To remove rows of data from a dataframe based on multiple conditional statements. We use square brackets [ ] with the dataframe and put multiple conditional statements along with AND or OR operator inside it. This slices the dataframe and removes all the rows that do not satisfy the given conditions.

How do you delete certain rows in Python?

To delete a row from a DataFrame, use the drop() method and set the index label as the parameter.

Which function is used to delete row from a data frame?

The Pandas “drop” function is used to delete columns or rows from a Pandas DataFrame.


1 Answers

You need the %in% operator. So,

df1[!(df1$name %in% df2$name),] 

should give you what you want.

  • df1$name %in% df2$name tests whether the values in df1$name are in df2$name
  • The ! operator reverses the result.
like image 111
csgillespie Avatar answered Oct 31 '22 17:10

csgillespie