Utilizing isin(): A Deep Dive into Efficient Data Filtering with Pandas

Pandas is a popular Python library for data analysis and manipulation. One of the essential tools in pandas is the DataFrame, a two-dimensional size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns). In this article, we delve into the isin() function, a versatile method for filtering data in a pandas DataFrame.

What is isin() in Pandas?

link to this section

The isin() function in pandas is used to filter data frames. It helps to filter the rows in a DataFrame based on a condition, which matches data in one or more columns. This function is very useful when you need to filter data based on multiple values of a column.

Syntax of isin()

link to this section
DataFrame.isin(values) 
  • values : A set of values or a dictionary where you can pass the column name(s) as the key(s) and the respective values that you want to filter.
Datathreads Advertisement - On-Premise ETL,BI, and AI Platform

Using isin() with a List of Values

link to this section

Suppose you have a DataFrame, and you want to filter rows based on certain values in a specific column. You can pass a list of values to the isin() function.

import pandas as pd 
    
data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Edward'], 
    'Age': [25, 30, 35, 40, 45], 
    'City': ['New York', 'San Francisco', 'Los Angeles', 'Chicago', 'Houston']} 
    
df = pd.DataFrame(data) 
filtered_df = df[df['Name'].isin(['Alice', 'Charlie', 'Edward'])] 
print(filtered_df) 

This will return a DataFrame with the rows where the 'Name' column matches 'Alice', 'Charlie', or 'Edward'.

Datathreads Advertisement - On-Premise ETL,BI, and AI Platform

Using isin() with a Dictionary

link to this section

You can also use isin() with a dictionary to filter based on multiple columns.

filter_conditions = { 
    'Name': ['Alice', 'Charlie', 'Edward'], 
    'City': ['New York', 'Los Angeles'] 
} 

filtered_df = df[df.isin(filter_conditions).all(1)] 
print(filtered_df) 

This example filters the DataFrame to include rows where 'Name' is 'Alice', 'Charlie', or 'Edward' AND 'City' is 'New York' or 'Los Angeles'.

Datathreads Advertisement - On-Premise ETL,BI, and AI Platform

Using isin() with a Series

link to this section

You can use isin() to compare a Series with another Series.

cities = pd.Series(['New York', 'Chicago', 'Houston']) 
filtered_df = df[df['City'].isin(cities)] 
print(filtered_df) 

This returns all rows where the 'City' matches any value in the cities Series.

Conclusion

link to this section

The isin() function is a powerful and flexible tool in pandas, providing a straightforward way to filter DataFrame rows based on various conditions. By mastering isin() , you can streamline your data analysis workflows, making your code more concise and readable. Whether you’re dealing with large datasets or small ones, understanding how to use isin() effectively is a valuable skill for any data analyst or scientist. Happy analyzing!