Created
August 9, 2016 05:56
-
-
Save tonybaloney/50b53c4347e00e5e8a3613843aaffba6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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