1

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?

1
  • You've already found it, but using the first match (if matched) only by using str[0]. Commented Aug 20, 2020 at 4:55

1 Answer 1

1

Try findall or extractall:

df.col1.str.findall(f'({"|".join(loc_list)})', flags=re.IGNORECASE)

df.col1.str.extractall(f'({"|".join(loc_list)})', flags=re.IGNORECASE)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.