# 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