【发布时间】:2017-10-30 16:13:31
【问题描述】:
我在 pandas df 中有一列标记化、词形还原的文本。我正在尝试创建一个词频矩阵,以便我可以继续进行降维。
我一直遇到一个错误,即 Python 需要一个字符串但得到一个列表。
TypeError: sequence item 0: expected str instance, list found
我尝试了几种方法,但每次都遇到错误。我不确定如何计算列表。
以下是我尝试过的一些方法:
选项 1:
from collections import Counter
df['new_col'] = Counter()
for token in df['col']:
counts[token.orth_] += 1
这会生成ValueError: Length of values does not match length of index
选项 2:
Counter(' '.join(df['col']).split()).most_common()
其中生成:TypeError: sequence item 0: expected str instance, list found
选项 3:
pd.Series(values = ','.join([(i) for i in df['col']]).lower().split()).value_counts()[:]
再次生成:TypeError: sequence item 0: expected str instance, list found
编辑:示例数据:
col
[indicate, after, each, action, step, .]
[during, september, and, october, please, refrain]
[the, work, will, be, ongoing, throughout, the]
[professional, development, session, will, be]
【问题讨论】:
-
如果您希望解决该问题,请通过示例提供可重现的错误。现在,您要求社区花费精力尝试对导致错误的数据帧进行逆向工程......
-
你能放一个
df['col']的样本 -
@Bharath 添加了一个示例
-
@LMGagne,您已经完成了部分工作,但请考虑使用 CountVectorizer,尤其是在您词汇量很大的情况下。
-
您能否查看现有答案并在他们回答您的问题时批准它们或进一步澄清您的需求?谢谢。
标签: python python-3.x pandas nlp spacy