I have a DataFrame and I need to create a new column and fill the values acording to how many words in a list of words are found in a text. I’m trying de code below:
JavaScript
x
15
15
1
df = pd.DataFrame({'item': ['a1', 'a2', 'a3'],
2
'text': ['water, rainbow', 'blue, red, white','country,school,magic']})
3
4
5
list_of_words = ['water', 'pasta', 'black', 'magic', 'glasses', 'school' ,'book']
6
7
for index,row in df.iterrows():
8
text = row['text']
9
count_found_words = 0
10
for word in list_of_words:
11
found_words= re.findall(word, text)
12
if len(found_words)>0:
13
count_found_words += 1
14
df['found_words'] = count_found_words
15
This code actually create a new column, but fill all the rows with the last ‘count_found_words’ of the loop.
is there a right way to do this?
Advertisement
Answer
JavaScript
1
4
1
pattern = fr"b({'|'.join(list_of_words)})b"
2
3
df["found_words"] = df.text.str.findall(pattern).str.len()
4
This forms the regex b(water|pasta|black|magic|glasses|school|book)b
that looks for any of the words in the list. Finds all it could and reports the number of matches via .len
.