python - 如何制作用于电影评论数据集分类的数据框?

标签 python pandas dataframe svm sentiment-analysis

我是 pandas 的新手,正在尝试使用一些数据进行练习。我得到以下格式的训练数据集。
这是电影评论的数据集。如何从此类数据中制作 DataFrame 以用于 SVM 分类。我已经用 [12000*12] 大小的数据进行分类练习,其中每行具有相同数量的属性。但在这里,属性的长度不相等。我该如何修改这个。

PhraseId    SentenceId  Phrase  Sentiment
1   1   Wanker Goths are on the loose ! 2
2   1   Wanker Goths    2
3   1   Wanker  2
4   1   Goths   2
5   1   are on the loose !  2
6   1   are on the loose    2
7   1   on the loose    2
8   1   the loose   2
9   2   made Eddie Murphy a movie star and the man has n't aged a day . 3
10  2   made Eddie Murphy a movie star and the man  3
11  2   Eddie Murphy a movie star and the man   2
12  2   a movie star and the man    2
13  2   a movie star and    2
14  2   has n't aged a day .    2
15  2   has n't aged a day  3
16  2   aged a day  2

这是实际训练dataset (部分)。

我的目标是通过数字数据映射从该数据集中形成一个数据帧,以便我可以使用该数据帧对情绪进行分类。

最佳答案

使用纯Python:

t = """PhraseId    SentenceId  Phrase  Sentiment
1   1   Wanker Goths are on the loose ! 2
2   1   Wanker Goths    2
3   1   Wanker  2
4   1   Goths   2
5   1   are on the loose !  2"""

按换行符分割字符串:

t = t.split('\n')

然后获取分割字符串的列表:

s = [i.split() for i in t]

然后合并短语并获取数据框:

import pandas as pd
df = pd.DataFrame([(i[0],i[1],' '.join(i[2:-1]),i[-1]) for i in s],columns=s[0])
df = df.ix[1:]
print df

关于python - 如何制作用于电影评论数据集分类的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27590796/

相关文章:

python - 如何使用先前字典中的键和值创建字典?

python - Scrapy 选择直接子项

python - 如何避免 pandas 在保存的 csv 中创建索引

python - 在散点图中突出显示特定点(基于条件)

python-2.7 - Pyspark 按另一个数据帧的列过滤数据帧

python - 从日期 python 中提取年/月到新列

r - 错误: nrow(x) == n is not TRUE when using Train in Caret

python - 提前执行的del语句

python - 我如何用 Nan 读取 pandas 中的 CSV 文件?

Python:在列表中存储多个数据帧