从给定的 Pandas 系列中过滤包含至少两个元音的单词
在本文中,我们如何过滤给定系列中包含两个或多个元音的单词。在这里,我们将看到实现这一目标的两种方法。
示例 1:
在此示例中,我们将使用map()函数循环遍历系列并检查每个单词的元音计数是否大于或等于 2。 map基本上用于循环遍历系列,而Counter用于计算每个单词中元音的数量。
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])
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])
输出:
示例 2:
在此示例中,我们将使用带有正则表达式的Series.str.count()函数来循环遍历系列并检查每个单词的元音计数是否大于或等于 2。 (?i)用于启动不区分大小写模式,将大写字符转换为小写字符。我们需要将大写字符转换为小写,因为对于元音我们正在与小写元音进行比较,所以每当大写元音出现在Apple中时,我们需要将“A”转换为小写以进行正确比较。
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])
输出: