I have a DataFrame as the following.
import pandas as pd
import numpy as np
df = pd.DataFrame([['One person has died after two motorbikes crashed in the Bay of Plenty.', 'The crash occurred at 3.15pm on Bell Rd in Nukuhou south of Whakatāne police said.', 'Another person suffered minor injuries in the crash.', 'WorkSafe had been advised and the Serious Crash Unit was in attendance police said.',
'One person has died and another has minor injuries following a serious crash in Nukuhou near Whakatāne in Eastern Bay of Plenty.', 'The crash involved two motorcycles and was reported around 3.15pm.', 'The Serious Crash Unit and WorkSafe are attending the scene.']]).T
df.columns = ['col1']
I want to find whether the following list of strings is available "col1" in the DataFrame.
loc_list = ['Bay of Plenty', 'Bell Rd', 'Nukuhou']
The following is what I tried.
df['location_mapped_title'] = (df.col1.str
.findall('|'.join(loc_list))
.str[0])
col1 location_mapped_title
0 One person has died after two motorbikes crash... Bay of Plenty
1 The crash occurred at 3.15pm on Bell Rd in Nuk... Bell Rd
2 Another person suffered minor injuries in the ... NaN
3 WorkSafe had been advised and the Serious Cras... NaN
4 One person has died and another has minor inju... Nukuhou
5 The crash involved two motorcycles and was rep... NaN
6 The Serious Crash Unit and WorkSafe are attend... NaN
But it doesn't print all the matched substrings. For example, at index 4 there is another "Bay of Plenty". How to find all the matching?
str[0].