I do I remove all rows in a dataframe where a certain row meets a string match criteria?
For example:
A,B,C 4,3,Foo 2,3,Bar 7,5,Zap
How would I return a dataframe that excludes all rows where C = Foo:
A,B,C 2,3,Bar 7,5,Zap
Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).
If we prefer to work with the Tidyverse package, we can use the filter() function to remove (or select) rows based on values in a column (conditionally, that is, and the same as using subset). Furthermore, we can also use the function slice() from dplyr to remove rows based on the index.
Just use the ==
with the negation symbol (!
). If dtfm is the name of your data.frame:
dtfm[!dtfm$C == "Foo", ]
Or, to move the negation in the comparison:
dtfm[dtfm$C != "Foo", ]
Or, even shorter using subset()
:
subset(dtfm, C!="Foo")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With