Table of Contents
はじめに
自然言語処理の前処理で役立つメモです。
df はこのようなイメージです。
方法
id | title | text |
---|---|---|
0 | hoge | I am so happy. |
1 | fuga | NaN |
2 | test | Hello World!! |
3 | yo | He is 13 years old. |
import nltknltk.download('stopwords')from nltk.corpus import stopwordsimport pandas as pd
df = pd.read_csv('df.csv', header=0)
# NULLを埋めるdf['text'].fillna('NULL', inplace=True)
# titleとtextを結合してconcat列を作成。NULLはスペースに置換df['concat'] = df['title'] + ' ' + df['text']df['concat'].replace('NULL', '', inplace=True)
# id列、title列、text列の削除df = df.drop(['id', 'title', 'text'], axis=1)
# 小文字にするdf['concat'] = df['concat'].str.lower()
# 数字は全てゼロにするdf['concat'] = df['concat'].replace('\d', '0', regex=True)
# 英数字と半角スペース以外を削除train_df['concat'] = train_df['concat'].str.replace('[^\w\s]','', regex=True)
# ストップワード除去stop_words= stopwords.words('english')def remove\_stopwords(words): words_nostop= ' '.join([word for word in words.split() if word not in stop_words]) return words_nostop
df['concat\_nostop'] = df['concat'].apply(lambda x: remove_stopwords(x))