Search code examples
pythonstringstring-matchingdata-extraction

Returning entries from a data set that contain a specific string in Python


I have a data set called df with the information of the name of the Drugs that has been administered. The column "drug_name" consists of all the various drug names. I would like to extract entries that only contain a specific name of the drugs.

I have tried the .str.contain method

df_adr= df[df["drug_name"].str.contains("epinephrine")==True]

This works but the problem is it returns entries that has the drug name "Norepinephrine" too because the word contains my key word "epinephrine". How do I get extract the entries that is an exact string match and not contain the string?


Solution

  • You may want to use a regular expression with word boundaries '\b'.

    df.loc[df["drug_name"].str.contains(r"\bepinephrine\b", case=False)]