Removes a word from a dataframe column based on a specific word list .txt

Viewed 24

I have a specific dataframe column like the one below.

Index Positif 
1     keren banget mobilnya
2     bagus kendaraannya keren deh
3     mobilnya baik jalannya
4     suara mesinnya indah dan baik

and I have a list of words based on .txt contains.

keren
bagus
baik
indah

I want the columns only to contain the specific words that are based on the .txt file and desired output.

Index Positif 
1     keren 
2     bagus keren
3     baik
4     indah baik
1 Answers

Try:

words = ["keren", "bagus", "baik", "indah"]

df["Positif"] = df.index.map(
    df["Positif"]
    .str.extractall("(" + "|".join(words) + ")")
    .groupby(level=0)
    .agg(" ".join)[0]
)
print(df)

Prints:

   Index      Positif
0      1        keren
1      2  bagus keren
2      3         baik
3      4   indah baik
Related