Exploring COVID-19 Tweet Sentiments and Consumer Behavior with Plotly

AhmedBilalUmer
5 min readApr 4, 2024

--

In the wake of the COVID-19 pandemic, understanding public sentiments and consumer behavior has never been more crucial. The Corona_NLP_train.csv dataset offers a window into the thoughts and actions of individuals during these challenging times. In this blog post, we’ll embark on a journey of exploration using Plotly to visualize and analyze the rich tapestry of data at our disposal.

Sentiment Analysis: Unveiling the Mood of the Masses

Sentiment analysis allows us to gauge public perceptions and attitudes towards COVID-19. Plotly Express comes to our aid, enabling us to create an insightful visualization titled “Sentiment Counts”. This dynamic bar chart vividly portrays the distribution of sentiments within the dataset, ranging from positive to negative. Through this visualization, we gain valuable insights into the prevailing mood of the masses, capturing the spectrum of emotions evoked by the pandemic.

Tracking Sentiments Over Time: A Chronicle of Evolving Trends

The pandemic is a dynamic phenomenon, characterized by shifting sentiments and evolving trends. With Plotly Express, we track sentiments over time through a compelling line chart titled “Sentiment Over Time”. This visualization provides a chronological narrative, revealing fluctuations in sentiment frequencies across different dates. As we delve deeper, we uncover nuanced patterns and temporal variations, shedding light on the evolving sentiments of individuals throughout the pandemic’s timeline.

Word Clouds: Painting a Picture with Words

Words have power, and word clouds offer a captivating glimpse into the collective consciousness. Using the WordCloud library, we generate word clouds for various sentiment categories — “Positive”, “Negative”, “Neutral”, “Extremely Positive”, and “Extremely Negative”. These visually stunning representations showcase the most frequently occurring words within each sentiment category, providing insights into the prevailing themes and concerns of individuals. From words of resilience to expressions of fear, the word clouds encapsulate the multifaceted nature of human emotions during times of crisis.

Decoding Consumer Behavior: Insights from Keyword Analysis

Consumer behavior undergoes seismic shifts in the face of crises like COVID-19. With Plotly Graph Objects, we embark on an exploration of consumer behavior through keyword analysis. Our visualization, titled “Consumer Behavior Analysis”, tracks trends related to keywords such as “shopping”, “online”, “food”, “panic buying”, “masks”, and “sanitizer”. Through this analysis, we unravel the dynamic landscape of consumer preferences and behaviors, uncovering patterns that illuminate the socio-economic impacts of the pandemic on purchasing habits and priorities.

Code

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
from wordcloud import WordCloud

# Load data from CSV with specified encoding
df = pd.read_csv("Corona_NLP_train.csv", encoding='latin1')

# Initialize Dash app
app = dash.Dash(__name__)

# Sentiment Analysis
sentiment_counts = df['Sentiment'].value_counts()
sentiment_counts_fig = px.bar(sentiment_counts, x=sentiment_counts.index, y=sentiment_counts.values,
color=sentiment_counts.index, labels={'x': 'Sentiment', 'y': 'Tweet Count'})

# Sentiment Over Time
df['TweetAt'] = pd.to_datetime(df['TweetAt'], format='%d-%m-%Y')
sentiment_over_time = df.groupby(['TweetAt', 'Sentiment']).size().reset_index(name='count')
sentiment_over_time_fig = px.line(sentiment_over_time, x='TweetAt', y='count', color='Sentiment',
labels={'TweetAt': 'Date', 'count': 'Tweet Count'})

# Word Cloud
def generate_word_cloud(sentiment):
text = ' '.join(df[df['Sentiment'] == sentiment]['OriginalTweet'])
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(text)
return wordcloud.to_image()

# Consumer Behavior Analysis
keywords = ['shopping', 'online', 'food', 'panic buying', 'masks', 'sanitizer']
consumer_behavior_data = {keyword: df['OriginalTweet'].str.count(keyword) for keyword in keywords}
consumer_behavior_fig = go.Figure()
for keyword, data in consumer_behavior_data.items():
consumer_behavior_fig.add_trace(go.Scatter(x=df['TweetAt'], y=data, mode='lines', name=keyword))

# Define Dash layout
# Define Dash layout
app.layout = html.Div([
html.H2("Sentiment Counts"),
dcc.Graph(id='sentiment-counts', figure=sentiment_counts_fig),

html.H2("Sentiment Over Time"),
dcc.Graph(id='sentiment-over-time', figure=sentiment_over_time_fig),

html.H2("Word Clouds"),
html.H3("Positive"),
html.Img(id='word-cloud-positive', src=generate_word_cloud('Positive')),

html.H3("Negative"),
html.Img(id='word-cloud-negative', src=generate_word_cloud('Negative')),

html.H3("Neutral"),
html.Img(id='word-cloud-neutral', src=generate_word_cloud('Neutral')),

html.H3("Extremely Positive"),
html.Img(id='word-cloud-extremely-positive', src=generate_word_cloud('Extremely Positive')),

html.H3("Extremely Negative"),
html.Img(id='word-cloud-extremely-negative', src=generate_word_cloud('Extremely Negative')),

html.H2("Consumer Behavior Analysis"),
dcc.Graph(id='consumer-behavior', figure=consumer_behavior_fig),
])


if __name__ == '__main__':
app.run_server(debug=True)

Conclusion: Harnessing Data Insights for Actionable Solutions

As our journey of exploration comes to an end, we reflect on the power of data-driven insights in navigating the complexities of the COVID-19 pandemic. Through the lens of Plotly visualizations, we’ve gained a deeper understanding of public sentiments and consumer behavior, laying the groundwork for informed decision-making and actionable solutions. As we continue to grapple with the challenges posed by the pandemic, let us harness the power of data to drive positive change and resilience in the face of adversity.

By leveraging the power of Plotly, we’ve embarked on a journey of exploration through the intricate web of COVID-19 sentiments and consumer behavior. Join us as we unravel the stories hidden within the data, one visualization at a time. If you have any suggestions, you can reach out to me @ https://www.linkedin.com/in/ahmed-bilal-umer-03a864241/

--

--