在列中加入单词列表

时间:2019-01-15 10:25:31

标签: python pandas join

是否可以在熊猫中加入单词?我有一个单词列表,我想再次将它们变成短语

数据

0    [hello, she, can, seem, to, form, something, like, a, coherent,...
1    [not, any, more,...
2    [it, is, unclear, if, any, better, deal,...
3    [but, few, in, her, party, seem, inclined ...
4    [it, is, unclear, if, the, basic, conditions, for, any,...
Name: Data, dtype: object

stop_words = set(stopwords.words('english'))

#new words
new_stopwords = {'hello'}

new_list = stop_words.union(new_stopwords)

#remove from NLTK stop list
not_stopwords = {'no', 'not, 'any'}

stopwords_list = set([word for word in new_list if word not in not_stopwords])

df['Data'] = df['Data'].' '.join([wrd for wrd in Data if wrd not in stopwords_list])

输出:

File "<ipython-input-281-498b9daa386f>", line 1
    df['Description_pretraites'] = df['Description_pretraites'].' '.join([wrd for wrd in replace_hour_token if wrd not in stopwords_list])
                                                              ^
SyntaxError: invalid syntax

好的输出

0    [can seem form something like coherent...
1    [not any more...
2    [is unclear any better deal...
3    [few party seem inclined ...
4    [is unclear basic conditions any...
Name: Data, dtype: object

从我所见,在熊猫中,该联接可用于连接列。但是是否可以在一个列中进行联接?

1 个答案:

答案 0 :(得分:2)

<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="600"> <tr> <td style="padding: 10px;"> <img src="http://example.com/example.png" width="100" height="50" alt="" border="0"> </td> </tr> </table> 与生成器一起使用:

.apply

或嵌套列表理解:

df['Data']=df['Data'].apply(lambda x: ' '.join(wrd for wrd in x if wrd not in stopwords_list))

示例

df['Data'] =  [' '.join(wrd for wrd in x if wrd not in stopwords_list) for x in df['Data']]