Присоединиться к списку слов в столбце - PullRequest
0 голосов
/ 15 января 2019

Возможно ли объединить слова в пандах? У меня есть список слов, и я пытаюсь снова превратить их в фразу

Данные

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 голосов
/ 15 января 2019

Используйте .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']]

Sample

d = {'Data':[['hello', 'she', 'can'],
             ['not', 'no', 'more', 'to']]}
df = pd.DataFrame(data=d)
print (df)
                  Data
0    [hello, she, can]
1  [not, no, more, to]

stopwords_list = set(['no','not'])
df['Data'] =  [' '.join(wrd for wrd in x if wrd not in stopwords_list) for x in df['Data']]
print (df)
            Data
0  hello she can
1        more to
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...