Sling Academy
Home/Pandas/Pandas: Filter elements of a Series based on a condition

Pandas: Filter elements of a Series based on a condition

Last updated: February 18, 2024

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.

Next Article: Utilizing the pandas.Series.bfill() method (4 examples)

Previous Article: Pandas: Adding prefix/suffix to index labels of a Series

Series: Pandas Series: From Basic to Advanced

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)