我有DataFrame
以下内容。
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']
我想查找以下字符串列表是否在中的“ col1”可用DataFrame
。
loc_list = ['Bay of Plenty', 'Bell Rd', 'Nukuhou']
以下是我尝试过的。
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
但是它不会打印所有匹配的子字符串。例如,在索引4处有另一个“丰盛湾”。如何找到所有匹配项?
尝试findall
或extractall
:
df.col1.str.findall(f'({"|".join(loc_list)})', flags=re.IGNORECASE)
df.col1.str.extractall(f'({"|".join(loc_list)})', flags=re.IGNORECASE)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句