Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove strings present in a list from a column in pandas

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
like image 652
Archit Avatar asked Aug 03 '18 06:08

Archit


1 Answers

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']]
like image 184
jezrael Avatar answered Oct 06 '22 00:10

jezrael