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

后端 未结 6 2065
谎友^
谎友^ 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:04

    I had to get rid of the NULL values before using the command recommended by Andy above. An example:

    df = pd.DataFrame(index = [0, 1, 2], columns=['first', 'second', 'third'])
    df.ix[:, 'first'] = 'myword'
    df.ix[0, 'second'] = 'myword'
    df.ix[2, 'second'] = 'myword'
    df.ix[1, 'third'] = 'myword'
    df
    
        first   second  third
    0   myword  myword   NaN
    1   myword  NaN      myword 
    2   myword  myword   NaN
    

    Now running the command:

    ~df["second"].str.contains(word)
    

    I get the following error:

    TypeError: bad operand type for unary ~: 'float'
    

    I got rid of the NULL values using dropna() or fillna() first and retried the command with no problem.

提交回复
热议问题