Filter an exact word in a dataframe (Pandas)

1

Code:

texto = ['El', 'enfermo', 'grave', 'habla', 'de', 'trasplantes', '.']
for palabra in texto:
    filtro = data[data['palabra'].str.match(palabra, case=False )]
    print(filtro)

An extract of the answer I get from the dataframe is:

       palabra      tag
374         el   DA0MS0
389         el   DA0MS0
398  elementos  NCMP000

What I need is that only the exact word "the" in this case, also filter all the words that contain "the" at the beginning as the word "elements" in the example.

Expected response:

       palabra      tag
374         el   DA0MS0
389         el   DA0MS0
    
asked by Ricardo Prieto 16.10.2018 в 01:19
source

1 answer

1

The solution was obtained by adding a regular expel (regex) + r '\ b' at the end of the word to be compared:

filtro = data[data['palabra'].str.match(palabra+ r'\b', case=False )]

More info: link

    
answered by 16.10.2018 / 02:24
source