Home > Net >  using list of word in function pandas extract
using list of word in function pandas extract

Time:10-31

I have list of word to search in dataframe with regex. I try to another way to use function extract without writing all the list in braket. Any idea plz?

df["description"].str.extract("(SECTION.?\dRADÔME|PROFONDEUR ET TAB|PRINCIPAL GAUCHE|PRINCIPAL DROIT|PLAN FIXE VERTICAL|PLAN FIXE HORIZONTAL|MOTEUR|KARMAN|HÉLICE|GOUVERNAIL ET TAB|CÔNE ARRIÈRE)")

I try to use join but it doesn't work.

CodePudding user response:

Use a list of words and create a pattern to use with extract:

words = [r'SECTION.?\dRADÔME',
        'PROFONDEUR ET TAB',
        'PRINCIPAL GAUCHE',
        'PRINCIPAL DROIT',
        'PLAN FIXE VERTICAL',
        'PLAN FIXE HORIZONTAL',
        'MOTEUR',
        'KARMAN',
        'HÉLICE',
        'GOUVERNAIL ET TAB',
        'CÔNE ARRIÈRE']

pattern = fr"({'|'.join(words)})"

df["description"].str.extract(pattern)
  • Related