【问题标题】:No attribute "str" on dataframe when creating a plot创建绘图时数据框上没有属性“str”
【发布时间】:2021-04-28 07:20:02
【问题描述】:

在情感分析后,我过滤了 5 条最大极性的推文。

maxx = df.nlargest(5,['polarity']).astype(str)
maxx

输出:

Unnamed: 0  clean_tweet Tweet_tokenized polarity    subjectivity    Sentiment_Type  scores  compound    sentiment_type  pca
315 315 Best of। luck Biden????   best / luck biden   1.0 0.3 POSITIVE    {'neg': 0.0, 'neu': 0.122, 'pos': 0.878, 'comp...   0.802   POSITIVE    [-0.06151099614792966, -0.030998756958434074]

现在我想创建一些 wordcloud 但我遇到了错误:

hero.wordcloud(maxx, max_words=100)
AttributeError: 'DataFrame' object has no attribute 'str'

【问题讨论】:

  • 你用什么包来绘制 wordcloud
  • texthero 我正在尝试

标签: python pandas string dataframe nlp


【解决方案1】:

按极性排序

df['polarity'] = df['polarity'].astype('float')   
maxx = df.nlargest(5, 'polarity')

如果你使用wordcloud 包试试这个

from wordcloud import WordCloud

text_data = ' '.join(maxx['clen_tweet']) 

wordcloud = WordCloud().generate(text_data)
plt.imshow(wordcloud2)
plt.axis("off")
plt.show()

使用texthero

import texthero as hero

hero.wordcloud(maxx['clean_tweet'], max_words=100)

【讨论】:

  • 谢谢,但我只想要 5 个最大的极性推文。对于所有推文都有效
  • @whyimnotwhy,我希望数据框“maxx”有 5 个最大的极性推文。使用该数据框进行绘图将起作用。请查看更新后的答案。
  • 尝试 maxx = df.nlargest(5, 'polarity') 根据极性对数据帧进行排序
猜你喜欢
  • 2019-08-18
  • 2018-07-11
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
相关资源
最近更新 更多