
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to filter perfect squares in a given series
Input −
Assume you have a series,
0 14 1 16 2 30 3 49 4 80
Output −
The result for perfect square elements are,
0 4 1 16 3 49
Solution 1
We can use regular expression and lambda function filter method to find the perfect square values.
Define a Series.
Apply lambda filter method to check the value is a perfect square or not. It is defined below,
l = [14,16,30,49,80] data=pd.Series([14,16,30,49,80]) result =pd.Series(filter(lambda x: x==int(m.sqrt(x)+0.5)**2,l))
Finally, check the list of values to the series using isin() function.
Example
import pandas as pd import math as m l = [4,16,30,49,80] data = pd.Series(l) print(data) lis = [] for i in range(len(data)): for j in data: if(data[i]==int(m.sqrt(j)+0.5)**2): lis.append(data[i]) print(“Perfect squares in the series: \n”, data[data.isin(lis)])
Output
Given series: 0 4 1 16 2 30 3 49 4 80 dtype: int64 Perfect Squares in the series: 0 4 1 16 3 49 dtype: int64
Solution 2
Example
import re import math as m l = [14,16,30,49,80] data = pd.Series([14,16,30,49,80]) print(“Given series:\n”, data) result = pd.Series(filter(lambda x: x==int(m.sqrt(x)+0.5)**2,l)) print(data[data.isin(result)])
Output
Given series: 0 14 1 16 2 30 3 49 4 80 dtype: int64 Perfect squares: 1 16 3 49 dtype: int64
- Related Articles
- Write a program in Python to filter valid dates in a given series
- Write a program in Python to filter armstrong numbers in a given series
- Write a program in Python to filter only integer elements in a given series
- Perfect Squares in C++
- Program to count number of perfect squares are added up to form a number in C++
- Write a Python program to shuffle all the elements in a given series
- Write a program in Python to filter the elements in a series which contains a string start and endswith ‘a’
- Write a program in Python to round all the elements in a given series
- Is a number sum of two perfect squares in JavaScript
- Write a program in Python to slice substrings from each element in a given series
- Get the Least squares fit of Laguerre series to data in Python
- Get the Least squares fit of Chebyshev series to data in Python
- Get the Least squares fit of Hermite series to data in Python
- Get the Least squares fit of Legendre series to data in Python
- Get the Least squares fit of Hermite_e series to data in Python

Advertisements