# Write a program in Python to remove the elements in a series, if it contains exactly two spaces

PythonPandasServer Side ProgrammingProgramming

Input

Assume, you have a series,

0    This is pandas
1    python script
2    pandas series

Output

And, the result after removing an element contains exactly two spaces,

1    python script
2    pandas series

## Solution 1

• Define a Series.

• Create lambda filter method to apply a regular expression to find the total number of spaces not equal to 2 as follows −

pd.Series(filter(lambda x:len(re.findall(r" ",x))!=2,data))
• Finally, check the list of values to the series using isin().

## Solution 2

• Define a Series.

• Create for loop to iter the elements one by one and set if condition to count the spaces equal to 2. If the element is matched, pop the particular value. It is defined below,

for i,j in data.items():
if(j.count(' ')==2):
data.pop(i)

### Example

Let us see the following implementation to get a better understanding.

import pandas as pd
import re
l = ["This is pandas","python script","pandas series"]
data = pd.Series(l)
result = pd.Series(filter(lambda x:len(re.findall(r" ",x))!=2,data))
print(data[data.isin(result)])

### Output

1    python script
2    pandas series
dtype: object

## Solution 3

### Example

import pandas as pd
l = ["This is pandas","python script","pandas Series"]
data = pd.Series(l)
for i,j in data.items():
if(j.count(' ')==2):
data.pop(i)
print(data)

### Output

1    python script
2    pandas series
dtype: object