How to check if Pandas column has value from list of string? - GeeksforGeeks

28 Nov, 2021
In this article, we are going to see how to check if the pandas column has a value from a list of strings in Python.
List of strings means a list contains strings as elements, we will check if the pandas dataframe has values from a list of strings and display them when they are present. We will get the dataframe columns where the strings in the list contain.
Python3
#import pandasimport pandas  # create dataframedata = pandas.DataFrame({'name': ['sireesha', 'priyank',                                   'ojaswi', 'gnanesh'],                          'subjects': ['java', 'networks',                                      'c', 'c#']})  # displaydata
Output:
In this scenario, the isin() function check the pandas column containing the string present in the list and return the column values when present, otherwise it will not select the dataframe columns.
Syntax: dataframe[dataframe[‘column_name’].isin(list_of_strings)]
where
dataframe is the input dataframe
list_of_strings is the list that contains strings
column_name is the column to check the list of strings present in that column
Example: Python program to check if pandas column  has a value from a list of strings
Python3
#import pandasimport pandas  # create dataframedata = pandas.DataFrame({'name': ['sireesha', 'priyank',                                  'ojaswi', 'gnanesh'],                         'subjects': ['java', 'networks',                                      'c', 'c#']})  # consider a listlist1 = ['sireesha', 'priyank']  # check the pandas name column# contain the given list if stringsprint(data[data['name'].isin(list1)])  # consider a listlist2 = ['java', 'c']  # check the pandas subjects column# contain the given list if stringsprint(data[data['subjects'].isin(list2)])
Output:
Here NumPy also uses isin() operator to check if pandas column has a value from a list of strings.
Syntax: dataframe[~numpy.isin(dataframe[‘column’], list_of_value)]
Example:
Python3
# import pandasimport pandas  # import numpyimport numpy  # create dataframedata = pandas.DataFrame({'name': ['sireesha', 'priyank',                                  'ojaswi', 'gnanesh'],                          'subjects': ['java', 'networks',                                       'c', 'c#']})  # consider a listlist1 = ['sireesha', 'priyank']  # check the pandas name column# contain the given list if stringsprint(data[data['name'].isin(list1)])  # consider a listlist2 = ['java', 'c']  # check the pandas subjects column# contain the given list if stringsdata[~numpy.isin(data['subjects'], list1)]
Output:
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
Python OOPs Concepts
Python | Get unique values from a list
Check if element exists in list in Python
Python Classes and Objects
Python | os.path.join() method
How To Convert Python Dictionary To JSON?
Python | Pandas dataframe.groupby()
Create a directory in Python