{"id":5694,"date":"2021-05-16T10:44:18","date_gmt":"2021-05-16T05:14:18","guid":{"rendered":"https:\/\/python-programs.com\/?p=5694"},"modified":"2021-11-22T18:45:26","modified_gmt":"2021-11-22T13:15:26","slug":"pandas-check-if-a-value-exists-in-a-dataframe-using-in-not-in-operator-isin","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-check-if-a-value-exists-in-a-dataframe-using-in-not-in-operator-isin\/","title":{"rendered":"Pandas : Check if a value exists in a DataFrame using in & not in operator | isin()"},"content":{"rendered":"

How to check if a value exists in a DataFrame using in and not in operator | isin() in Python ?<\/h2>\n

In this article we are going to discuss different ways to check if a value is present in the dataframe.<\/p>\n

We will be using the following dataset as example<\/p>\n

       Name\u00a0       Age\u00a0\u00a0\u00a0\u00a0\u00a0   City\u00a0          Marks\r\n0\u00a0\u00a0\u00a0\u00a0 Jill\u00a0\u00a0           16\u00a0\u00a0\u00a0\u00a0     Tokyo\u00a0\u00a0\u00a0         154\r\n1\u00a0\u00a0   Rachel\u00a0\u00a0     38\u00a0\u00a0\u00a0\u00a0    Texas\u00a0\u00a0\u00a0          170\r\n2\u00a0\u00a0\u00a0  Kirti\u00a0\u00a0         39\u00a0        New York\u00a0\u00a0\u00a0\u00a0  88\r\n3\u00a0\u00a0\u00a0  Veena\u00a0\u00a0      40\u00a0\u00a0\u00a0\u00a0    Texas\u00a0\u00a0\u00a0         190\r\n4\u00a0    Lucifer\u00a0\u00a0     35\u00a0\u00a0\u00a0\u00a0    Texas\u00a0\u00a0\u00a0\u00a0         59\r\n5\u00a0\u00a0\u00a0  Pablo\u00a0\u00a0      30\u00a0       New York\u00a0\u00a0\u00a0    123\r\n6\u00a0\u00a0   Lionel\u00a0\u00a0      45\u00a0       Colombia\u00a0\u00a0\u00a0   189<\/pre>\n

Check if a single element exists in DataFrame using in & not in operators :<\/h3>\n

Dataframe class has a member Dataframe.values<\/code> \u00a0that gives us all the values in an numpy representation. We will be using that with the in and not operator to check if the value is present in the dataframe.<\/p>\n

Using in operator to check if an element exists in dataframe :<\/h4>\n

We will be checking if the value 190 exists in the dataset using in<\/code> operator.<\/p>\n

#Program :\r\n\r\nimport pandas as pd\r\n#Example data\r\nemployees = [\r\n('Jill',    16,     'Tokyo',    154),\r\n('Rachel',  38,     'Texas',    170),\r\n('Kirti',   39,     'New York',  88),\r\n('Veena',   40,     'Texas',    190),\r\n('Lucifer', 35, 'Texas',     59),\r\n('Pablo',   30,     'New York', 123),\r\n('Lionel',  45,     'Colombia', 189)]\r\n\r\n# DataFrame object was created\r\ndfObj = pd.DataFrame(employees, columns=['Name', 'Age', 'City', 'Marks'])\r\nif 190 in dfObj.values:\r\n    print('Element exists in Dataframe')\r\n<\/pre>\n
Output :\r\nElement exists in Dataframe<\/pre>\n

Using not in operator to check if an element doesn\u2019t exists in dataframe :<\/h4>\n

We will be checking if \u2018Leo\u2019 is present in the dataframe using not<\/code> operator.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\n#Example data\r\nemployees = [\r\n('Jill',    16,     'Tokyo',    154),\r\n('Rachel',  38,     'Texas',    170),\r\n('Kirti',   39,     'New York',  88),\r\n('Veena',   40,     'Texas',    190),\r\n('Lucifer', 35, 'Texas',     59),\r\n('Pablo',   30,     'New York', 123),\r\n('Lionel',  45,     'Colombia', 189)]\r\n# DataFrame object was created\r\ndfObj = pd.DataFrame(employees, columns=['Name', 'Age', 'City', 'Marks'])\r\nif 'Leo' not in dfObj.values:\r\n    print('Element does not exists in Dataframe')\r\n\r\n<\/pre>\n
Output :\r\nElement does not exists in Dataframe<\/pre>\n

Checking if multiple elements exists in DataFrame or not using in operator :<\/strong><\/h4>\n

To check for multiple elements, we have to write a function.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\n\r\ndef checkForValues(_dfObj, listOfValues):\r\n    #The function would check for the list of values in our dataset\r\n    result = {}\r\n    #Iterate through the elementes\r\n    for elem in listOfValues:\r\n        # Check if the element exists in the dataframe values\r\n        if elem in _dfObj.values:\r\n            result[elem] = True\r\n        else:\r\n            result[elem] = False\r\n    # Returns a dictionary containig the vvalues and their existence in boolean        \r\n    return result\r\n\r\n#Example data\r\nemployees = [\r\n('Jill',    16,     'Tokyo',    154),\r\n('Rachel',  38,     'Texas',    170),\r\n('Kirti',   39,     'New York',  88),\r\n('Veena',   40,     'Texas',    190),\r\n('Lucifer', 35,     'Texas',     59),\r\n('Pablo',   30,     'New York', 123),\r\n('Lionel',  45,     'Colombia', 189)]\r\n# DataFrame object was created\r\ndfObj = pd.DataFrame(employees, columns=['Name', 'Age', 'City', 'Marks'])\r\n\r\n#Check for the existence of values\r\nTresult = checkForValues(dfObj, [30, 'leo', 190])\r\nprint('The values existence inside the dataframe are ')\r\nprint(Tresult)\r\n<\/pre>\n
Output :\r\nThe values existence inside the dataframe are\r\n{30: True, 'leo': False, 190: True}<\/pre>\n

Rather than writing a whole function, we can also achieve this using a smaller method using dictionary comprehension.<\/p>\n

# Program :\r\n\r\nimport pandas as pd\r\n\r\n#Example data\r\nemployees = [\r\n('Jill',    16,     'Tokyo',    154),\r\n('Rachel',  38,     'Texas',    170),\r\n('Kirti',   39,     'New York',  88),\r\n('Veena',   40,     'Texas',    190),\r\n('Lucifer', 35,     'Texas',     59),\r\n('Pablo',   30,     'New York', 123),\r\n('Lionel',  45,     'Colombia', 189)]\r\n# DataFrame object was created\r\ndfObj = pd.DataFrame(employees, columns=['Name', 'Age', 'City', 'Marks'])\r\nlistOfValues = [30, 'leo', 190]\r\n#using dictionary comprehension check for given values\r\nresult = {elem: True if elem in dfObj.values else False for elem in listOfValues}\r\nprint('The values existence inside the dataframe are ')\r\nprint(result)\r\n<\/pre>\n
Output :\r\nThe values existence inside the dataframe are\r\n{30: True, 'leo': False, 190: True}<\/pre>\n

Checking if elements exists in DataFrame using isin() function :<\/h3>\n

We can also check if a value exists inside a dataframe or not using the isin( )<\/code> function.<\/p>\n

Syntax : DataFrame.isin(self, values)<\/pre>\n

Where,<\/p>\n