Check if string is in a pandas dataframe

前端 未结 7 2055
北荒
北荒 2020-11-28 06:38

I would like to see if a particular string exists in a particular column within my dataframe.

I\'m getting the error

ValueError: The truth v

7条回答
  •  长情又很酷
    2020-11-28 07:02

    You should use any()

    In [98]: a['Names'].str.contains('Mel').any()
    Out[98]: True
    
    In [99]: if a['Names'].str.contains('Mel').any():
       ....:     print "Mel is there"
       ....:
    Mel is there
    

    a['Names'].str.contains('Mel') gives you a series of bool values

    In [100]: a['Names'].str.contains('Mel')
    Out[100]:
    0    False
    1    False
    2    False
    3    False
    4     True
    Name: Names, dtype: bool
    

提交回复
热议问题