DataFrame - 删除 'word' 列中包含停用词或数字的行
DataFrame - remove rows that contains stop words or numbers in the 'word' column
我有两列(单词,并计算这些单词的数量)的数据框,按计数排序:
Word Count
0 the 5337
1 boy 895
2 who 5891
3 lived 3150
4 mr 3443
... ... ...
6049 manner 3256
6050 holiday 2702
6051 347 276
6052 spreading 4937
6053 348 277
我想要的是删除停用词和数字(如“347”和“348”)。例如在示例中,删除第 0、2、6051、6053 行('the'、'who'、'347'、'348')。
这就是我创建 DataFrame 的方式:
count_words_dict = {'the': 5337, 'boy': 895, 'who': 5891, 'lived': 3150, 'mr': 3443, 'and': 462, 'mrs': 3444, 'dursley': 1797, 'of': 3618, 'number': 3599, 'four': 2240, 'privet': 4007, 'drive': 1749, 'were': 5842, 'proud': 4034, 'to': 5431, 'say': 4397, 'that': 5336, 'they': 5346}
df = pd.DataFrame(list(count_words_dict.items()), columns = ['Word','Count'])
df.sort_values(by=['Count'], ascending=False)
df.reset_index(drop=True)
我也得到了停用词:
!pip install nltk
import nltk
nltk.download("stopwords")
from nltk.corpus import stopwords
stops = set(stopwords.words('english'))
但是如何从 DataFrame 中删除那些停用词(最好是数字)?
我在 this blog post 中看到他们设法从特朗普的推文数据集中删除了停用词,但我没能使他的代码在我的数据集上运行。这是他的代码:
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
stops = set(stopwords.words('english')+['com'])
co = CountVectorizer(stop_words=stops)
counts = co.fit_transform(data.Tweet_Text)
pd.DataFrame(counts.sum(axis=0),columns=co.get_feature_names()).T.sort_values(0,ascending=False).head(50)
使用pandas.Series.isin
with pandas.Series.str.isnumeric
首先从stopwords
列表中删除匹配的单词,然后从Word
列中排除数值:
count_words_dict = {'the': 5337, 'boy': 895, 'who': 5891, 'lived': 3150, 'mr': 3443, 'and': 462, 'mrs': 3444, 'dursley': 1797,
'of': 3618, 'number': 3599, 'four': 2240, 'privet': 4007, 'drive': 1749, 'were': 5842, 'proud': 4034,
'to': 5431, 'say': 4397, 'that': 5336, 'they': 5346, '345':200, '555':1000}
df = pd.DataFrame(list(count_words_dict.items()), columns = ['Word','Count'])
df.sort_values(by=['Count'], ascending=False)
df.reset_index(drop=True)
from nltk.corpus import stopwords
stops = list(stopwords.words('english'))
df = df[~df['Word'].isin(stops)]
df = df[~df['Word'].str.isnumeric()]
我有两列(单词,并计算这些单词的数量)的数据框,按计数排序:
Word Count
0 the 5337
1 boy 895
2 who 5891
3 lived 3150
4 mr 3443
... ... ...
6049 manner 3256
6050 holiday 2702
6051 347 276
6052 spreading 4937
6053 348 277
我想要的是删除停用词和数字(如“347”和“348”)。例如在示例中,删除第 0、2、6051、6053 行('the'、'who'、'347'、'348')。
这就是我创建 DataFrame 的方式:
count_words_dict = {'the': 5337, 'boy': 895, 'who': 5891, 'lived': 3150, 'mr': 3443, 'and': 462, 'mrs': 3444, 'dursley': 1797, 'of': 3618, 'number': 3599, 'four': 2240, 'privet': 4007, 'drive': 1749, 'were': 5842, 'proud': 4034, 'to': 5431, 'say': 4397, 'that': 5336, 'they': 5346}
df = pd.DataFrame(list(count_words_dict.items()), columns = ['Word','Count'])
df.sort_values(by=['Count'], ascending=False)
df.reset_index(drop=True)
我也得到了停用词:
!pip install nltk
import nltk
nltk.download("stopwords")
from nltk.corpus import stopwords
stops = set(stopwords.words('english'))
但是如何从 DataFrame 中删除那些停用词(最好是数字)?
我在 this blog post 中看到他们设法从特朗普的推文数据集中删除了停用词,但我没能使他的代码在我的数据集上运行。这是他的代码:
from sklearn.feature_extraction.text import CountVectorizer
from nltk.corpus import stopwords
stops = set(stopwords.words('english')+['com'])
co = CountVectorizer(stop_words=stops)
counts = co.fit_transform(data.Tweet_Text)
pd.DataFrame(counts.sum(axis=0),columns=co.get_feature_names()).T.sort_values(0,ascending=False).head(50)
使用pandas.Series.isin
with pandas.Series.str.isnumeric
首先从stopwords
列表中删除匹配的单词,然后从Word
列中排除数值:
count_words_dict = {'the': 5337, 'boy': 895, 'who': 5891, 'lived': 3150, 'mr': 3443, 'and': 462, 'mrs': 3444, 'dursley': 1797,
'of': 3618, 'number': 3599, 'four': 2240, 'privet': 4007, 'drive': 1749, 'were': 5842, 'proud': 4034,
'to': 5431, 'say': 4397, 'that': 5336, 'they': 5346, '345':200, '555':1000}
df = pd.DataFrame(list(count_words_dict.items()), columns = ['Word','Count'])
df.sort_values(by=['Count'], ascending=False)
df.reset_index(drop=True)
from nltk.corpus import stopwords
stops = list(stopwords.words('english'))
df = df[~df['Word'].isin(stops)]
df = df[~df['Word'].str.isnumeric()]