{"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":"
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>\nCheck 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>\nOutput :\r\nElement exists in Dataframe<\/pre>\nUsing 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>\nOutput :\r\nElement does not exists in Dataframe<\/pre>\nChecking 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>\nOutput :\r\nThe values existence inside the dataframe are\r\n{30: True, 'leo': False, 190: True}<\/pre>\nRather 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>\nOutput :\r\nThe values existence inside the dataframe are\r\n{30: True, 'leo': False, 190: True}<\/pre>\nChecking 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>\nWhere,<\/p>\n
\n
- Values :<\/strong> It takes the values to check for inside the dataframe.<\/li>\n<\/ul>\n
Checking if a single element exist in Dataframe using isin() :<\/h4>\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\n#Checking ofr a single element susing isin( ) function\r\nboolDf = dfObj.isin([38])\r\nprint(boolDf)\r\n<\/pre>\nOutput :\r\n \u00a0Name\u00a0 Age\u00a0\u00a0 City\u00a0 Marks\r\n0\u00a0 False\u00a0 False\u00a0 False\u00a0 False\r\n1\u00a0 False\u00a0\u00a0 True\u00a0 False\u00a0 False\r\n2\u00a0 False\u00a0 False\u00a0 False\u00a0 False\r\n3\u00a0 False\u00a0 False\u00a0 False\u00a0 False\r\n4\u00a0 False\u00a0 False\u00a0 False\u00a0 False\r\n5\u00a0 False\u00a0 False\u00a0 False\u00a0 False\r\n6\u00a0 False\u00a0 False\u00a0 False\u00a0 False<\/pre>\nHere the
isin( )<\/code> operator returned a boolean dataframe of the same number of elements, where the elements that matched our values is True and rest all are false.<\/p>\n
We can add this to the
any( )<\/code> function that only shows the true values and pass it into another
any( )<\/code> function making it a series to pinpoint if our element exists or not.<\/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\n# Check if the value is inisde the dataframe using both isin() and any( ) function\r\nresult = dfObj.isin(['Lionel']).any().any()\r\nif result:\r\n print('The value exists inside the datframe')\r\n<\/pre>\nOutput :\r\nAny of the Element exists in Dataframe<\/pre>\nChecking if any of the given values exists in the Dataframe :<\/h4>\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\n# Check if all the values are inisde the dataframe using both isin() and any( ) function\r\nresult = dfObj.isin([81, 'Lionel', 190,]).any().any()\r\nif result:\r\n print('Any of the Element exists in Dataframe')\r\n<\/pre>\nOutput :\r\nAny of the Element exists in Dataframe<\/pre>\nThis program only prints if any of the given values are existing inside the dataframe.<\/p>\n
Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n
Read more Articles on Python Data Analysis Using Padas \u2013 Find Elements in a Dataframe<\/strong><\/p>\n
\n
- Find & Drop duplicate columns in a DataFrame<\/a><\/li>\n
- Check if a DataFrame is empty in Python<\/a><\/li>\n
- Find duplicate rows in a Dataframe based on all or selected columns using DataFrame.duplicated() in Python<\/a><\/li>\n
- Find maximum values & position in columns or rows of a Dataframe<\/a><\/li>\n
- Find indexes of an element in pandas dataframe<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"
How to check if a value exists in a DataFrame using in and not in operator | isin() in Python ? In this article we are going to discuss different ways to check if a value is present in the dataframe. We will be using the following dataset as example Name\u00a0 Age\u00a0\u00a0\u00a0\u00a0\u00a0 City\u00a0 Marks 0\u00a0\u00a0\u00a0\u00a0 …<\/p>\n