#python-3.x #pandas #jupyter-notebook #tf-idf #stop-words
#python-3.x #pandas #jupyter-ноутбук #tf-idf #стоп-слова
Вопрос:
Я пытаюсь удалить пользовательский список стоп-слов, но он не работает.
desc = pd.DataFrame(description, columns =['description'])
print(desc)
Что дает следующие результаты
description
188693 The Kentucky Cannabis Company and Bluegrass He...
181535 Ohio County Sheriff
11443 According to new reports from federal authorit...
213919 KANSAS CITY, Mo. (AP)The Chiefs will be withou...
171509 The crew of Insight, WCNY's weekly public affa...
... ...
2732 The Arkansas Supreme Court on Thursday cleared...
183367 Larry Pegram, co-owner of Pure Ohio Wellness, ...
134291 Joe Biden will spend the next five months pres...
239270 Find out where your Texas representatives stan...
246070 SAN TAN VALLEY — Two men have been charged wit...
[9875 rows x 1 columns]
Я нашел здесь следующий код, но, похоже, он не работает
remove_words = ["marijuana", "cannabis", "hemp", "thc", "cbd"]
pat = '|'.join([r'b{}b'.format(w) for w in remove_words])
desc.assign(new_desc=desc.replace(dict(string={pat: ''}), regex=True))
Что приводит к следующим результатам
description new_desc
188693 The Kentucky Cannabis Company and Bluegrass He... The Kentucky Cannabis Company and Bluegrass He...
181535 Ohio County Sheriff Ohio County Sheriff
11443 According to new reports from federal authorit... According to new reports from federal authorit...
213919 KANSAS CITY, Mo. (AP)The Chiefs will be withou... KANSAS CITY, Mo. (AP)The Chiefs will be withou...
171509 The crew of Insight, WCNY's weekly public affa... The crew of Insight, WCNY's weekly public affa...
... ... ...
2732 The Arkansas Supreme Court on Thursday cleared... The Arkansas Supreme Court on Thursday cleared...
183367 Larry Pegram, co-owner of Pure Ohio Wellness, ... Larry Pegram, co-owner of Pure Ohio Wellness, ...
134291 Joe Biden will spend the next five months pres... Joe Biden will spend the next five months pres...
239270 Find out where your Texas representatives stan... Find out where your Texas representatives stan...
246070 SAN TAN VALLEY — Two men have been charged wit... SAN TAN VALLEY — Two men have been charged wit...
9875 rows × 2 columns
Как вы можете видеть, стоп-слова не были удалены. Мы будем признательны за любую помощь, которую вы можете предоставить.
Ответ №1:
Обращайтесь с делом, упрощайте выкройку,
remove_words = ["marijuana", "cannabis", "hemp", "thc", "cbd"]
pat = '|'.join(remove_words)
desc['new_desc'] = desc.description.str.lower().replace(pat,'', regex=True)
description new_desc
0 The Kentucky Cannabis Company and Bluegrass He... the kentucky company and bluegrass he...
1 Ohio County Sheriff ohio county sheriff
2 According to new reports from federal authorit... according to new reports from federal authorit...
3 KANSAS CITY, Mo. (AP)The Chiefs will be mariju... kansas city, mo. (ap)the chiefs will be witho...
4 The crew of Insight, WCNY's weekly public affa... the crew of insight, wcny's weekly public affa...
Комментарии:
1. Это вроде сработало, но я не уверен, что смогу объяснить, что это сделало. Похоже, что он помещает ‘new_desc’ в тот же столбец, что и исходный ‘desc’ df. Это не то, что я хотел сделать. Я просто хотел удалить пользовательские стоп-слова.
2. @MaxxABillion, приведенный выше код создает новый столбец с именем new_desc. Не уверен, что вы подразумеваете под «он помещает ‘new_desc’ в тот же столбец, что и исходный ‘desc’ df»
3. Я понял это. Он отображался странно. Я удалил старый столбец и был готов к работе. Все еще столкнулся с другой проблемой. Скоро я опубликую еще один вопрос