w3resource

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

 

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 =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

Explanation:

color_series = pd.Series(['Red', 'Green', 'Orange', 'Pink', 'Yellow', 'White']): This line creates a Pandas Series object 'color_series' containing six strings representing different colors.

result = color_series.map(lambda c: sum([Counter(c.lower()).get(i, 0) for i in list('aeiou')]) >= 2): This code applies the map() method to the Pandas Series object 'color_series' and a lambda function that counts the number of vowels in each string using the Counter() function from the Python collections module. The lambda function then checks if the number of vowels in each string is greater than or equal to 2.

The resulting Pandas Series object 'result' contains boolean values corresponding to each string in 'color_series'. Each boolean value is True if the corresponding string has at least 2 vowels, and False otherwise.

Python-Pandas 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 Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.