Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to filter perfect squares in a given series
A perfect square is a number that can be expressed as the product of an integer with itself. In this tutorial, we'll learn how to filter perfect squares from a Pandas Series using different approaches.
Given a series with numbers, we want to identify and filter only the perfect squares ?
Input and Expected Output
Input −
0 14 1 16 2 30 3 49 4 80
Output −
1 16 3 49
Method 1: Using Mathematical Check with sqrt()
We can check if a number is a perfect square by taking its square root and verifying if it's an integer ?
import pandas as pd
import math
# Create a series
data = pd.Series([14, 16, 30, 49, 80])
print("Given series:")
print(data)
# Find perfect squares using mathematical approach
perfect_squares = []
for num in data:
sqrt_val = math.sqrt(num)
if sqrt_val == int(sqrt_val):
perfect_squares.append(num)
# Filter the series
result = data[data.isin(perfect_squares)]
print("\nPerfect squares in the series:")
print(result)
Given series: 0 14 1 16 2 30 3 49 4 80 dtype: int64 Perfect squares in the series: 1 16 3 49 dtype: int64
Method 2: Using Lambda Function with Filter
A more concise approach using lambda function and the mathematical check ?
import pandas as pd
import math
# Create a series
data = pd.Series([14, 16, 30, 49, 80])
print("Given series:")
print(data)
# Use lambda function to filter perfect squares
perfect_squares = list(filter(lambda x: x == int(math.sqrt(x) + 0.5)**2, data))
result = data[data.isin(perfect_squares)]
print("\nPerfect squares in the series:")
print(result)
Given series: 0 14 1 16 2 30 3 49 4 80 dtype: int64 Perfect squares in the series: 1 16 3 49 dtype: int64
Method 3: Using Boolean Indexing
A direct approach using boolean indexing with Pandas ?
import pandas as pd
import math
# Create a series
data = pd.Series([14, 16, 30, 49, 80])
print("Given series:")
print(data)
# Create boolean mask for perfect squares
is_perfect_square = data.apply(lambda x: math.sqrt(x) == int(math.sqrt(x)))
result = data[is_perfect_square]
print("\nPerfect squares in the series:")
print(result)
Given series: 0 14 1 16 2 30 3 49 4 80 dtype: int64 Perfect squares in the series: 1 16 3 49 dtype: int64
How It Works
The key concept is checking if the square root of a number is an integer:
Mathematical approach: Calculate
sqrt(x)and check if it equalsint(sqrt(x))Alternative check: Verify if
x == int(sqrt(x) + 0.5)**2(handles floating point precision)Boolean indexing: Use
apply()with a lambda function to create a boolean mask
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| Mathematical Check | High | Good | Understanding the logic |
| Lambda Filter | Medium | Good | Functional programming style |
| Boolean Indexing | High | Best | Pandas-native operations |
Conclusion
Use boolean indexing with apply() for the most Pandas-native approach. The mathematical check using sqrt() is the most straightforward method for filtering perfect squares from a series.
