Python 从熊猫数据帧中删除停用词会给出错误的输出



我从多个文件中删除了停用词。首先,我读取每个文件并从数据帧中删除停用词。之后,我将数据帧与下一个数据帧连接起来。当我打印数据帧时,它给我一个输出,如下所示:

0      [I,  ,  ,  ,  , r, e,  , h,  , h,  , h, v, e, ...      
1      [D,  , u,  , e, v, e, n,  , e,  , h, e,  , u, ...     
2      [R, g, h,  , f, r,  , h, e,  , e, c, r,  , w, ...     
3      [A, f, e, r,  , c, l, l, n, g,  , n,  , p, l, ...     
4      [T, h, e, r, e,  , v, e, r, e, e, n,  ,  , n, ...   

这是我的代码:

allFiles = glob.glob(ROOT_DIR + '/' + DATASET + "/*.csv")
frame = pd.DataFrame()
list_ = []
stop = stopwords.words('english') 
for file_ in allFiles:
    chunkDataframe = pd.read_csv(file_,index_col=None, header=0, chunksize=1000)
    dataframe = pd.concat(chunkDataframe, ignore_index=True)
    dataframe['Text'] = dataframe['Text'].apply(lambda x: [item for item in x if item not in stop])
    print dataframe
    list_.append(dataframe)
frame = pd.concat(list_)

请帮助我优化读取多个文件的方式,并从中删除停用词。

dataframe['Text']包含一个字符串,而不是单词列表。因此,如果您使用lambda x: [item for item in x if item not in stop]迭代它,则可以逐个字符地迭代它并生成字符列表。要逐字迭代,请将其更改为:

lambda x: [item for item in string.split(x) if item not in stop]

相关内容

最新更新