【问题标题】:How to create a bag of words from csv file in python?如何在 python 中从 csv 文件创建一个单词包?
【发布时间】:2017-12-22 04:33:00
【问题描述】:

我是 python 新手。我有一个已清理推文的 csv 文件。我想用这些推文创建一个词袋。 我有以下代码,但它不能正常工作。

import pandas as pd
from sklearn import svm
from sklearn.feature_extraction.text import CountVectorizer

data = pd.read_csv(open("Twidb11.csv"), sep=' ')
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(data.Text)
count_vect.vocabulary_

错误:

.ParserError:标记数据时出错。 C 错误:预期有 19 个字段 第 5 行,锯 22

【问题讨论】:

  • 澄清错误发生在代码中的确切位置会很有用...
  • 现在运行代码时出现此错误:'DataFrame' object has no attribute 'Text'

标签: python-2.7 machine-learning sentiment-analysis


【解决方案1】:

我认为它是重复的。你可以看到答案here。有很多答案和cmets。

所以,解决方案可以是:

data = pd.read_csv('Twidb11.csv', error_bad_lines=False)

或者:

df = pandas.read_csv(fileName, sep='delimiter', header=None)

“在上面的代码中, sep 定义了您的分隔符,而 header=None 告诉熊猫您的源数据没有标题/列标题行。因此,文档说:“如果文件不包含标题行,那么您应该明确传递header=None"。在这种情况下,pandas 会自动为每个字段 {0,1,2,...} 创建整数索引。"

【讨论】:

  • 现在运行代码时出现此错误:'DataFrame' object has no attribute 'Text'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
相关资源
最近更新 更多