Search code examples
pandasdataframepandas-datareader

How to check specific column have specific values or not in dataframe?


I want to check that, if column 'data' is contains abc and xyz only or not. If not then return the same. Expected output: False

 import pandas as pd
    data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42],'data':['abc','xyz','pqr','abc']}
    df = pd.DataFrame(data)
    print df

Solution

  • You can test sets if want test if exist only some values in any order:

    print (set(df['data']) == set(['xyz', 'abc']))
    False
    

    EDIT:

    If need True if match at least one value:

    data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42],
            'data':['abc','abc','abc','abc']}
    df = pd.DataFrame(data)
    print (df)
        Name  Age data
    0    Tom   28  abc
    1   Jack   34  abc
    2  Steve   29  abc
    3  Ricky   42  abc
    
    print (set(df['data']) <= set(['xyz', 'abc']))
    True