Pandas DataFrame.isin
Pandas DataFrame.isin
The DataFrame.isin
method in pandas is used to check whether each element in a DataFrame is contained in a specified set of values. It returns a DataFrame of the same shape with boolean values indicating the presence of each element in the provided set.
Syntax
The syntax for DataFrame.isin
is:
DataFrame.isin(values)
Here, DataFrame
refers to the pandas DataFrame being checked.
Parameters
Parameter | Description |
---|---|
values | A scalar, list, set, dictionary, or Series to check against. If a dictionary or Series is passed, the keys or index must match the columns of the DataFrame. |
Returns
A DataFrame of boolean values of the same shape as the input DataFrame, indicating whether each element is in values
.
Examples
Checking Membership in a List
Use isin
to check if elements in a DataFrame are present in a list of values.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'City': ['Delhi', 'Mumbai', 'Chennai']
}
df = pd.DataFrame(data)
# Check if elements are in the provided list
values_to_check = ['Arjun', 'Mumbai', 30]
print("Boolean DataFrame indicating membership in the list:")
print(df.isin(values_to_check))
Output
Boolean DataFrame indicating membership in the list:
Name Age City
0 True False False
1 False True True
2 False False False
Checking Membership Using a Dictionary
Use a dictionary to specify values for specific columns to check for membership.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'City': ['Delhi', 'Mumbai', 'Chennai']
}
df = pd.DataFrame(data)
# Check membership using a dictionary
values_to_check = {
'Name': ['Arjun', 'Priya'],
'City': ['Delhi']
}
print("Boolean DataFrame indicating membership using a dictionary:")
print(df.isin(values_to_check))
Output
Boolean DataFrame indicating membership using a dictionary:
Name Age City
0 True False True
1 False False False
2 True False False
Checking Membership Using a Set
You can also use a set to check for membership in a DataFrame.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'City': ['Delhi', 'Mumbai', 'Chennai']
}
df = pd.DataFrame(data)
# Check membership using a set
values_to_check = {'Arjun', 'Chennai', 25}
print("Boolean DataFrame indicating membership using a set:")
print(df.isin(values_to_check))
Output
Boolean DataFrame indicating membership using a set:
Name Age City
0 True True False
1 False False False
2 False False True
Using isin
for Filtering Rows
The isin
method is often used to filter rows based on conditions.
Python Program
import pandas as pd
# Create a DataFrame
data = {
'Name': ['Arjun', 'Ram', 'Priya'],
'Age': [25, 30, 35],
'City': ['Delhi', 'Mumbai', 'Chennai']
}
df = pd.DataFrame(data)
# Filter rows where 'City' is in the given list
filtered_df = df[df['City'].isin(['Delhi', 'Chennai'])]
print("Filtered DataFrame:")
print(filtered_df)
Output
Filtered DataFrame:
Name Age City
0 Arjun 25 Delhi
2 Priya 35 Chennai
Summary
In this tutorial, we explored the DataFrame.isin
method in pandas. Key takeaways include:
- Using
isin
to check element membership in a list, set, or dictionary. - Returning a boolean DataFrame indicating membership.
- Using
isin
for filtering rows based on conditions.
The DataFrame.isin
method is a versatile tool for checking membership in pandas DataFrames.