Pandas: How to get the modulo of 2 Series

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

Introduction

Working with data efficiently often requires performing various arithmetic operations. In this tutorial, we will focus on how to perform modulo operations between two Pandas Series. The modulo operation, also known as the remainder operation, is a fundamental mathematical operation. It finds extensive use in programming, data analysis, and data science to derive insights from data or to transform data into a more useful format.

First, let’s introduce the modulo operation. The modulo operation returns the remainder of a division of two numbers. In Python and Pandas, this operation is performed using the % operator.

Setting up Your Environment

Before we dive into the examples, make sure you have Pandas installed. If not, you can install it using pip:

pip install pandas

Also, import Pandas in your Python script or Jupyter notebook:

import pandas as pd

Basic Modulo Operation between Two Series

Let’s start with the basics. Here’s how you can create two Pandas Series and perform a modulo operation:

import pandas as pd

# Creating two Series
series1 = pd.Series([5, 10, 15, 20])
series2 = pd.Series([2, 3, 4, 5])

# Performing modulo operation
result = series1 % series2

print(result)

The output will be:

0    1
1    1
2    3
3    0
dtype: int64

This output shows the remainder of dividing each element of series1 by its corresponding element in series2.

Dealing with Missing Values

It’s common to encounter missing values when working with real-world datasets. Let’s see how to handle such cases:

import pandas as pd

# Creating two Series with missing values
series1 = pd.Series([10, None, 30, 40])
series2 = pd.Series([3, 5, None, 8])

# Performing modulo operation with fillna to handle missing values
result = series1.fillna(0) % series2.fillna(1)

print(result)

The output will demonstrate how missing values are treated as 0 in series1 and 1 in series2 to perform the modulo operation without errors.

Applying Modulo of Series to Filter Data

Modulo operations can be particularly useful for filtering data based on certain conditions. For example, to filter even and odd numbers:

import pandas as pd

# Creating a Series
series = pd.Series(range(1, 11))

# Filtering even numbers
even_numbers = series[series % 2 == 0]

# Filtering odd numbers
odd_numbers = series[series % 2 != 0]

print("Even Numbers:\n", even_numbers, "\nOdd Numbers:\n", odd_numbers)

This example shows how to use the modulo operation to filter even and odd numbers from a Series.

Advanced Use: Applying Functions with Modulo Operation

For more advanced data manipulation, you can apply functions that include modulo operations. The apply method can be used for this purpose:

import pandas as pd

# Function to determine if the number is divisible by a given divisor
def is_divisible_by(number, divisor=3):
    return number % divisor == 0

# Applying the function to a Series
series = pd.Series(range(1, 11))
result = series.apply(is_divisible_by, args=(3,))

print(result)

This code snippet showcases a user-defined function determining if numbers in a Series are divisible by 3, illustrating the versatility of the modulo operation when combined with apply.

Conclusion

The modulo operation is a powerful tool for performing arithmetic operations on Pandas Series. From basic arithmetic to handling missing values and even advanced data manipulation with custom functions, understanding how to apply the modulo operation can enhance data analysis skills significantly. We hope this tutorial has provided you with a comprehensive understanding of utilizing the modulo operation between two Pandas Series, paving the way for more efficient data processing and insight generation.