python - 带有字符串列表的列中的文本的 Wordcloud
问题描述
我的数据集有 10 列,其中一列有文本作为字符串列表。
数据集:
Col1 Col2 Col3 Text
... ... ... ['I','have', 'a','dream']
... ... ... ['My', 'mom', 'is','Spanish']
编码
wordcloud = WordCloud(stopwords=stopwords, max_font_size=50, max_words=100, background_color="white").generate(' '.join(df['Text']))
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
返回
TypeError: sequence item 0: expected str instance, list found
很明显,它需要字符串,而不是列表。如何在字符串中转换 Text 列中的列表?
解决方案
您可以尝试先用 连接列中的列表df['Text']
,.sum()
然后加入:
combined_text = ' '.join(df['Text'].sum())
wordcloud = (
WordCloud(stopwords=stopwords,
max_font_size=50,
max_words=100,
background_color="white")
.generate(combined_text)
)
推荐阅读
- javascript - 在javascript中按属性选择元素,其中属性名称有冒号?
- java - 有没有办法遍历 2 个 for 循环,使得 (i+j) > 3
- python - 用 python 和 IPython 小部件表示无穷大
- r - R data.table 函数根据同一行上的引用列号复制值
- runtime-error - pytorch:RuntimeError:具有多个值的张量的布尔值不明确
- oracle - Debezium CDC 连接器说 java.library.path 中没有 ocijdbc11
- pypy - 如何解决或报告 pathos 和 pypy 之间的不兼容问题
- python - 如何在不编写额外代码的情况下使用 Python 将“-”(破折号)读取为标准输入?
- javascript - 如何更改 Material-UI 的卡片标题正面?
- curl - Postgresql v12 函数在 Json 中返回函数名称