![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to check if Pandas column has value from list of string?
In pandas library, we create the data in the form of rows and columns. The columns can be of string data type. The list of strings means the list containing the string elements.
Pandas is one of the libraries in python which is abbreviated as Python Data Analysis Library. It is used to perform data analysis, data cleaning, data manipulations and scientific calculations. The data in pandas library is represented in columns and rows. It has many functions and modules which are used to perform data analysis and manipulations.
Python provides different ways to check if pandas column has value from list of string type. Let’s see each way one by one.
Using dataframe() function
Firstly, let’s create the sample data frame with the columns and rows. The following is the code.
import pandas as pd dic = {"Name":["Anil","Srivatsav","Prasad","Mitra"], "Class":[10,30,20,50], "subject":["python","Java","c","Android"]} data = pd.DataFrame(dic) print(data.head())
Output
Following is the data frame created with pandas –
Name Class subject 0 Anil 10 python 1 Srivatsav 30 Java 2 Prasad 20 c 3 Mitra 50 Android
Using the isin() function
The isin() is a function available in pandas library which is used to check whether the elements in the DataFrame or Series are present in the given list or, tuple or, array objects. Following is the syntax for using the isin() function.
DataFrame.isin(elements)
Example
In this example, we will apply the isin() function on a desired column by passing list of strings as the input arguments.
import pandas as pd dic = {"Name":["Anil","Srivatsav","Prasad","Mitra"], "Class":[10,30,20,50], "subject":["python","Java","c","Android"]} data = pd.DataFrame(dic) data.isin(["Anil","Srivatsav","Java"]) print(data.head())
Output
The output of program above is given below –
Name Class subject 0 Anil 10 python 1 Srivatsav 30 Java 2 Prasad 20 c 3 Mitra 50 Android
Using numpy isin() function
The NumPy library provides the function isin() which is as similar to the isin() function of the pandas library. We can pass any python object such as list, tuple or array objects etc., and check if the contents of the given object exists in the current data set.
np.isin(DataFrame,elements)
Where,
numpy and pandas are the libraries
isin is the function available in numpy
elements are the input data
DataFrame is the 2-d labeled data
Example
In this example, we will use the isin() function of the NumPy library to check if pandas column has value from list of strings.
import pandas as pd import numpy as np dic = {"Name":["Anil","Srivatsav","Prasad","Mitra"], "Class":[10,30,20,50], "subject":["python","Java","c","Android"]} data = pd.DataFrame(dic) output = np.isin(data["Name"],["Anil","Srivatsav"]) print(output)
Output
Following is the output of the isin function of the Numpy library.
[ True True False False]
Example
Let’s see another example to check if pandas column has value from list of strings using the isin() function of NumPy library.
import pandas as pd import numpy as np dic = {"Name":["Anil","Srivatsav","Prasad","Mitra"], "Class":[10,30,20,50], "subject":["python","Java","c","Android"]} data = pd.DataFrame(dic) output = np.isin([data["Name"],data["subject"]],["Anil","Srivatsav"]) print(output)
Output
[[ True True False False] [False False False False]]