w3resource

Python Pandas: Filter words from a given series that contain atleast two vowels

 

Python Pandas: Data Series Exercise-30 with Solution

Write a Pandas program to filter words from a given series that contain atleast two vowels.

Sample Solution :

Python Code :

import pandas as pd
from collections import Counter
color_series = pd.Series(['Red', 'Green', 'Orange', 'Pink', 'Yellow', 'White'])
print("Original Series:")
print(color_series)
print("\nFiltered words:")
result = mask = color_series.map(lambda c: sum([Counter(c.lower()).get(i, 0) for i in list('aeiou')]) >= 2)
print(color_series[result])

Sample Output:

Original Series:
0       Red
1     Green
2    Orange
3      Pink
4    Yellow
5     White
dtype: object

Filtered words:
1     Green
2    Orange
4    Yellow
5     White
dtype: object

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to convert year-month string to dates adding a specified day of the month.
Next: Write a Pandas program to compute the Euclidean distance between two given series.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Python: Membership Testing in a Collection

>>> a = ('one', 'two', 'three', 'four', 'five')
>>> if 'one' in a:
...     print('The tuple contains one.')
... 
The tuple contains one.
>>> b = {0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> if 2 in b.keys():
...     print('The dict has the key of 2.')
... 
The dict has the key of 2.