我正在寻找一种方法来获取数据帧中字符串列中唯一单词的列表。你知道吗
import pandas as pd
import numpy as np
df = pd.read_csv('FinalStemmedSentimentAnalysisDataset.csv', sep=';',dtype=
{'tweetId':int,'tweetText':str,'tweetDate':str,'sentimentLabel':int})
tweets = {}
tweets[0] = df[df['sentimentLabel'] == 0]
tweets[1] = df[df['sentimentLabel'] == 1]
我使用的数据集来自以下链接:http://thinknook.com/twitter-sentiment-analysis-training-corpus-dataset-2012-09-22/
我得到了一列长度可变的字符串,我想得到列中每个唯一单词的列表和它的计数,我如何得到它?我在python中使用Pandas,原始数据库有超过1M行,所以我还需要一些有效的方法来足够快地处理这个问题,并且不要让代码运行太长时间。你知道吗
列的示例可以是:
这首新歌是什么?你知道吗
名单可能是这样的。
[is,so,sad,for,my,apl,friend,omg,this,terrible,what,new,song]
如果列中有字符串,则必须将每个句子拆分为单词列表,然后将所有列表放在一个列表中-您可以使用它
sum()
为此,它应该为您提供所有单词。要获得唯一的单词,可以将其转换为set()
,然后再转换回list()
但在开始时,您必须清理句子以删除像
.
、?
等字符。我使用regex
只保留一些字符和空间。最后你必须把所有的单词都转换成小写或大写。你知道吗结果
编辑:如注释中提到的@HenryYik-
findall('\w+')
可以代替split()
,也可以代替replace()
编辑:我用来自
http://thinknook.com/twitter-sentiment-analysis-training-corpus-dataset-2012-09-22/
除了
column.sum()
或sum(column)
之外,所有的工作都很快—我测量了1000行的时间,计算了150万行的时间,需要35分钟。你知道吗使用
itertools.chain()
要快得多—大约需要8秒钟。你知道吗但是它可以直接转换成
set()
。你知道吗大约需要5秒钟
完整代码:
结果
相关问题 更多 >
编程相关推荐