Vega
Vega

Reputation: 2929

.query() for negated str.contains()?

I want to filter out every row where a column has a specific string.

If I want to filter every row that contains that string:

df.query("column_name.str.contains('stringtosearch')")

But if I want to use .query() to filter out every row containing that string I need to negate that str.contains().

I do not want to use df[~d["column_name"].str.contains("stringtosearch")] because I want a unified syntax and I use .query() everywhere else in my code.

How can I do this with .query()?

Upvotes: 2

Views: 1068

Answers (1)

BigBen
BigBen

Reputation: 50008

Simply with the ~ operator (or not) , as demonstrated in the query docs in indexing.

df.query("~column_name.str.contains('stringtosearch')")

Upvotes: 4

Related Questions