Search for “does-not-contain” on a DataFrame in pandas

后端 未结 6 2069
谎友^
谎友^ 2020-11-28 02:31

I\'ve done some searching and can\'t figure out how to filter a dataframe by df[\"col\"].str.contains(word), however I\'m wondering if there is a way to do the

6条回答
  •  死守一世寂寞
    2020-11-28 03:09

    You can use the invert (~) operator (which acts like a not for boolean data):

    new_df = df[~df["col"].str.contains(word)]
    

    , where new_df is the copy returned by RHS.

    contains also accepts a regular expression...


    If the above throws a ValueError, the reason is likely because you have mixed datatypes, so use na=False:

    new_df = df[~df["col"].str.contains(word, na=False)]
    

    Or,

    new_df = df[df["col"].str.contains(word) == False]
    

提交回复
热议问题