Pandas: Filter elements of a Series based on a condition

Updated: February 18, 2024 By: Guest Contributor Post a comment

Introduction

Filtering data is a fundamental operation when working with pandas, a powerful and flexible data processing and analysis library for Python. It’s common to need to select data that meets certain conditions, and pandas provides rich functionalities to perform these tasks efficiently. Today, we’ll focus specifically on filtering elements of a pandas Series based on one or more conditions. We’ll cover various examples, gradually moving from basic to more advanced use cases. Let’s dive in!

Getting Things Ready

Before moving to the examples, let’s make sure we have pandas installed and properly imported. If you haven’t installed pandas yet, you can do so using pip:

pip install pandas

Then, to use pandas in your script, import it as follows:

import pandas as pd

Basic Filtering

Let’s start with the basics. Imagine we have a Series with several integers, and we want to filter out the numbers that are less than 5.

import pandas as pd

# Sample Series
data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# Filter the Series
data_filtered = data[data < 5]

# Display the filtered Series
print(data_filtered)

Output:

0    1
1    2
2    3
3    4
dtype: int64

Using Logical Operators

Filtering with a single condition is straightforward, but you might often need to combine conditions. For example, what if we want to select numbers that are either less than 3 or greater than 8? We can achieve this using the logical operators & (and) and | (or).

import pandas as pd

# Again, let's create a Series
data = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# Use logical operators to filter
filtered_data = data[(data < 3) | (data > 8)]

# Output the result
print(filtered_data)

Output:

0     1
1     2
8     9
9    10
dtype: int64

Applying Functions for Filtering

Oftentimes, the condition for filtering might not be based on simple comparisons. This is where the apply() method becomes handy. The apply() method allows you to apply a function to each item in the series. For example, let’s filter our series to find numbers that are prime.

import pandas as pd

# Function to check if a number is prime
def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True

# Our series again
s = pd.Series(range(1, 11))

# Filtering with a custom function
primes = s[s.apply(is_prime)]

# Output
print(primes)

Output:

1     2
2     3
4     5
6     7
9    11
dtype: int64

Advanced Filtering: Using query() Method

For more complex data structures like DataFrames, the query() method can be a more readable and efficient way to filter data. Though it’s mainly associated with DataFrames, knowing how it works helps understand the broader spectrum of pandas’ filtering capabilities. Let’s look at an example of how it might apply in a slightly more complex scenario than a Series.

import pandas as pd
import numpy as np

# Creating a DataFrame with two columns
data = pd.DataFrame({
    'number': range(1, 11),
    'is_even': [True if i % 2 == 0 else False for i in range(1, 11)]
})

# Using query to filter
filtered_data = data.query('is_even == True')

# Output
print(filtered_data)

While this example shows a DataFrame, the logic and techniques we covered with Series are directly transferable and apply to DataFrame filtering as well.

Conclusion

Filtering data based on conditions is a common and essential part of data analysis with pandas. We have explored how to filter Series objects from basic comparisons to applying custom functions and even touched upon advanced filtering with the query() method for DataFrames. By mastering these techniques, you can begin to unlock the full potential of pandas in your data processing tasks.