如何解决词云在词中显示多个 ' 并且不知道为什么
我试图用“'”排除它们,但失败了。不确定他们从哪里拉,因为它们不在文档中。感谢您的帮助
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
url = 'https://raw.githubusercontent.com/Imme21/WordCloud/main/StockData3.csv'
df = pd.read_csv(url,error_bad_lines=False)
df.dropna(inplace = True)
text = df['Stock'].values
wordcloud = WordCloud(background_color = 'white',stopwords = ['Date','Stock','Tickers','Open','Close','High','Low','IV','under','over','price','change','%','null']).generate(str(text))
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
解决方法
发生这种情况是因为您有一个字符串数组,其中每个字符串都包含一个引号。 Wordcloud 假设尾随撇号可以是单词的一部分(因此它可以处理诸如 can't are not 之类的单词)。有关详细信息,请参阅 this post。
您可以通过使用空格分隔的单词字符串而不是列表来解决此问题。 ' '.join(text) 应该可以解决你的问题
# You read a sring from file and converted it to array
text = df['Stock'].values
>>> text
array(['GME','SPY','TSLA','PLTR','AAPL','AMC','RKT','NIO','DIS',.....
text = ' '.join(text)
>>> text
'GME SPY TSLA PLTR AAPL AMC RKT NIO.....
这应该注意词云中正确的尾随引号或撇号。
,问题与您如何从数据框列中的值获取字符串有关。具体来说,text = df['Stock'].values
和 .generate(str(text)
。
使用 pandas.Series.str.cat
将产生“正确”的字符串,并为您提供所需的结果:
...
>>> text = df['Stock'].str.cat(sep=' ')
...
>>> wordcloud = WordCloud(background_color = 'white',stopwords = ['Date','Stock','Tickers','Open','Close','High','Low','IV','under','over','price','change','%','null']).generate(text)
...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。