我想将 movie_reviews 数据集从 nltk.corpus 转换成数据框。 目的是使用此数据进行情绪分析。 使用 pandas 转换数据时出现错误:
from nltk.corpus import movie_reviews
import pandas as pd
mr=movie_reviews
movie=pd.DataFrame(mr)
ValueError: DataFrame constructor not properly called!
请您参考如下方法:
NLTK 的 CategorizedPlaintextCorpusReader 对象不是 pandas 的 dtype。
也就是说,您可以将电影评论转换为元组列表,然后填充数据框:
import pandas as pd
from nltk.corpus import movie_reviews as mr
reviews = []
for fileid in mr.fileids():
tag, filename = fileid.split('/')
reviews.append((filename, tag, mr.raw(fileid)))
df = pd.DataFrame(reviews, columns=['filename', 'tag', 'text'])
[输出]:
>>> df.head()
filename tag text
0 cv000_29416.txt neg plot : two teen couples go to a church party ,...
1 cv001_19502.txt neg the happy bastard's quick movie review \ndamn ...
2 cv002_17424.txt neg it is movies like these that make a jaded movi...
3 cv003_12683.txt neg " quest for camelot " is warner bros . ' firs...
4 cv004_12641.txt neg synopsis : a mentally unstable man undergoing ...
要处理 text 列,请参阅 How to NLTK word_tokenize to a Pandas dataframe for Twitter data?
