I have a dataframe df, 
import pandas as pd
df = pd.DataFrame(
    {
        "ID": [1, 2, 3, 4, 5],
        "name": [
            "Hello Kitty",
            "Hello Puppy",
            "It is an Helloexample",
            "for stackoverflow",
            "Hello World",
        ],
    }
)
which looks like:
   ID               name
0   1        Hello Kitty
1   2        Hello Puppy
2   3   It is an Helloexample
3   4  for stackoverflow
4   5        Hello World
I have a list of strings To_remove_list
To_remove_lst = ["Hello", "for", "an", "It"]
I need to remove all the strings present in the list from the column name of df. How can I do this in pandas ?
My expected answer is:
   ID               name
0   1              Kitty
1   2              Puppy
2   3              is example
3   4              stackoverflow
4   5              World
                I think need str.replace if want remove also substrings:
df['name'] = df['name'].str.replace('|'.join(To_remove_lst), '')
If possible some regex characters:
import re
df['name'] = df['name'].str.replace('|'.join(map(re.escape, To_remove_lst)), '')
print (df)
   ID            name
0   1           Kitty
1   2           Puppy
2   3     is  example
3   4   stackoverflow
4   5           World
But if want remove only words use nested list comprehension:
df['name'] = [' '.join([y for y in x.split() if y not in To_remove_lst]) for x in df['name']]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With