I have a pandas data frame and a list. I am trying to match the list of string with the data frame and fill a new column named system_name with the matched string or strings.
import pandas as pd
import re
df = pd.DataFrame({"A":["ailerons and landing gear is jammed.",
"something went wrong in flaps and slats. Flaps are open",
"engine is lost with flap and brake but right brake is working",
"I dont know anything about elevator and engine. Engine is lost "]})
systems = ['aileron', 'landing gear', 'flap', 'slat', 'engine', 'brake', 'elevator']
I tried with this code below:
df['system_name'] = df['A'].str.findall('(' + '|'.join(systems) + ')', flags=re.IGNORECASE).str.join(',')
and getting this outcome:
Here its extracting all matches. Instead of all matches, I want an element from the list to match once in every row in column A and show in the output. So for an example, for index 1, instead of flap, slat, Flap, the outcome should show flap, slat.