Search code examples
pythonpandastwitterword-cloud

Word Cloud in Python


Have a dataframe:1

Using the following code but get ValueError: We need at least 1 word to plot a word cloud, got 0.

Does anyone know how to fix this; I am trying to generate 3 word clouds one for all tweets one for real tweets and one for fake tweets.

Thank you for the help in advance :)

df_real = df[df['label']==1]

df_fake = df[df['label']==0]

tweet_All = " ".join(review for review in df.Tweet)

tweet_real = " ".join(review for review in df_real.Tweet)

tweet_fake = " ".join(review for review in df_fake.Tweet)

fig, ax = plt.subplots(3, 1, figsize  = (30,30))
# Create and generate a word cloud image:
wordcloud_ALL = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(tweet_All)
wordcloud_ADR = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(str(tweet_real))
wordcloud_NADR = WordCloud(max_font_size=50, max_words=100, background_color="white").generate(str(tweet_fake))

# Display the generated image:
ax[0].imshow(wordcloud_ALL, interpolation='bilinear')
ax[0].set_title('All Tweets', fontsize=30)
ax[0].axis('off')
ax[1].imshow(wordcloud_ADR, interpolation='bilinear')
ax[1].set_title('Tweets under real Class',fontsize=30)
ax[1].axis('off')
ax[2].imshow(wordcloud_NADR, interpolation='bilinear')
ax[2].set_title('Tweets under fake Class',fontsize=30)
ax[2].axis('off')

Solution

  • df['label'] seems to be of type str (or object): In your screenshot it shows "fake" as entry. Maybe df_real and df_fake are empty? I would try to show the head of df_real and df_fake.

    One solution could be to replace the first lines with

    df_real = df[df['label']!='fake']
    df_fake = df[df['label']=='fake']