Home »
Python »
Python Programs
How to check if a value is in the list in selection from pandas dataframe?
Learn, how to check if a value is in the list in selection from pandas dataframe?
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
Suppose that we are given pandas dataframe with some columns and we need to check if a value is present in the list in selection from this dataframe.
Checking if a value is in the list in selection from pandas data frame
For this purpose, we will use the isin() method. This method is used to check whether each element in the DataFrame is contained in values.
The result will only be true at a location if all the labels match. If the value is a Series, that's the index. If the value is a dict, the keys must be the column names, which must match. If the value is a DataFrame, then both the index and column labels must match.
Let us understand with the help of an example,
Python program to check if a value is in the list in selection from pandas dataframe
# Import pandas
import pandas as pd
# Import numpy
import numpy as np
# Creating a dataframe
df = pd.DataFrame({'A': [2, 4], 'B': [2, 0]},
index=['1', '2'])
# Display original dataframe
print("Original DataFrame:\n",df,"\n")
# Using isin method
res = df.isin([0, 2])
# Display result
print("Result:\n",res,"\n")
Output
The output of the above program is:
Python Pandas Programs »