Home > Net >  how to certain remove string from list in dataframe? very basic python
how to certain remove string from list in dataframe? very basic python

Time:04-11

I want to remove certain string from a list form in Dataframe.

I declared a list and I wanna remove these 3 words in dataframe.

Each sentence from list in dataframe is all separated as word and I don't know how to remove it...

lst = ['I','There','people']


|      Token                |   
|---------------------------|
|['I', 'like', 'apple']     |    
|['There', 'are', 'people'] |  
|['she','likes','apple']    |  

CodePudding user response:

Use:

list_ = ['I','There','people']

temp = pd.DataFrame({'Token':[
    ['I', 'like', 'apple'],
    ['There', 'are', 'people'],
    ['he','likes','apple']
]}) 
temp['Token'].apply(lambda x: [y for y in x if y not in list_])

Output:

0         [like, apple]
1                 [are]
2    [he, likes, apple]
Name: Token, dtype: object

CodePudding user response:

For efficiency, use a list comprehension:

S = set(lst)
temp['Token'] = [[w for w in x if w not in S]
                 for x in temp['Token']]

Output:

                Token
0       [like, apple]
1               [are]
2  [he, likes, apple]
  • Related