
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a program in Python to remove the elements in a series, if it contains exactly two spaces
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
- Related Questions & Answers
- Write a program in Python to check if a series contains duplicate elements or not
- Write a Python program to shuffle all the elements in a given series
- Write a program in Python to round 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 print the elements in a series between a specific range
- Write a program in Python to filter only integer elements in a given series
- Write a Python code to combine two given series and convert it to a dataframe
- Write a program in Python to print the power of all the elements in a given series
- How to write a Regular Expression in JavaScript to remove spaces?
- Write a program in Python to sort all the elements in a given series in descending order
- Write a program in Python to find the missing element in a given series and store the full elements in the same series
- C++ Program to remove spaces from a string?
- Write a program in Python to find the most repeated element in a series
- Write a program in C++ to split two strings to make it a palindrome
- Write a Python code to concatenate two Pandas series into a single series without repeating the index
Advertisements