Skip to content

Instantly share code, notes, and snippets.

@tonybaloney
Created August 9, 2016 05:56
Show Gist options
  • Save tonybaloney/50b53c4347e00e5e8a3613843aaffba6 to your computer and use it in GitHub Desktop.
Save tonybaloney/50b53c4347e00e5e8a3613843aaffba6 to your computer and use it in GitHub Desktop.
import pandas as pd
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from matplotlib import pyplot as plt
with open('tweets.csv') as tweets:
df = pd.read_csv(tweets)
data = []
sid = SentimentIntensityAnalyzer()
df['timestamp'] = pd.to_datetime(df['timestamp'])
df['weekday'] = df['timestamp'].apply(lambda x: x.weekday())
chart = []
for (sentence, id) in zip(df.text.values, df.weekday.values):
ss = sid.polarity_scores(sentence)
chart.append({'sentiment': ss['compound'], 'date': id})
df = pd.DataFrame.from_records(chart)
mean_weekday = df.groupby('date')['sentiment'].mean()
fig, ax = plt.subplots()
plt.bar([0,1,2,3,4,5,6], mean_weekday)
ax.set_xticklabels(('M', 'T', 'W', 'T', 'F', 'S', 'S'))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment