Skip to content
Related Articles

Related Articles

Filter words from a given Pandas series that contain atleast two vowels
  • Last Updated : 26 Jul, 2020

In this article, how we can filter the words from a given series which contain two or more vowels. Here we’ll see two ways in which we can achieve this.
Example 1:
In this examlple we’ll use the map() function to loop through the series and check for each word if the count of vowels is greater than or equal to two. map is basically used to loop through the series and Counter is used to count the number of vowels in each word. 
 

Python3




import pandas as pd
from collections import Counter
  
# creating a series of words
series = pd.Series(['Apple', 'Banana', 'Cherry'
                    'Plum', 'Orange', 'Fig', 'Melon'])
  
print("Original Series:")
print(series)
print("\nWords containing atleast 2 vowels")
  
# mapping through the series and checking if count of vowels is >=2
result = series.map(lambda c: sum([Counter(c.lower()).get(i, 0
                                   for i in list('aeiou')]) >= 2)
  
print(series[result])

Output:
 

Example 2:
In this examlple we’ll use the Series.str.count() function with regex to loop through the series and check for each word if the count of vowels is greater than or equal to two. (?i) is used to start case-insensitive mode, to convert the uppercase character to lowercase characters. We need to convert uppercase characters to lowercase because for vowels we are comparing with lowercase vowels, so whenever an uppercase vowels appers like in Apple we need to convert ‘A’ to lowercase for proper comparison. 
 



Python3




import pandas as pd
from collections import Counter
  
# creating a series of words
series = pd.Series(['Apple', 'Banana', 'Cherry',
                    'Plum', 'Orange', 'Fig', 'Melon'])
  
print("Original Series:")
print(series)
print("\nWords containing atleast 2 vowels")
  
# mapping through the series and checking 
# if count of vowels is >=2
result = series[series.str.count('(?i)[aeiou]') >=2]
  
print(series[result])

Output:
 

 

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :