Last active
April 7, 2017 20:20
-
-
Save WalkerHarrison/a8de72b002e83c77df86093319bbe3b0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
d = {"search": searches, | |
"time": dates} | |
googled = pd.DataFrame(d) | |
dt = datetime.datetime(2014, 10, 1) | |
end = datetime.datetime(2017, 3, 5) | |
step = datetime.timedelta(days=7) | |
weekly = [] | |
while dt < end: | |
weekly.append(dt.strftime('%Y-%m-%d %H:%M:%S')) | |
dt += step | |
# finding/smoothing/normalizing weekly data only shown for 'trump' but same process applies to the other terms | |
trump_weeks = [] | |
for i in range(len(weekly)-1): | |
trump_weeks.append(sum((googled['time'] > weekly[i]) & | |
(googled['time'] < weekly[i+1]) & | |
(googled['search'].str.contains('trump')))) | |
term = len(trump_weeks)-1 | |
trump_weeks_smooth = [(trump_weeks[i] + trump_weeks[i-1] +trump_weeks[i+1])/3 for i in range(1, term)] | |
trump_weeks_smooth_norm = [i/float(max(trump_weeks_smooth)) for i in trump_weeks_smooth] | |
plt.plot(range(term-1), trump_weeks_smooth_norm, label='trump', linewidth=5.0) | |
plt.plot(range(term-1), warriors_weeks_smooth_norm, label='warriors', linewidth=5.0) | |
plt.plot(range(term-1), ibm_weeks_smooth_norm, label='ibm', linewidth=5.0) | |
plt.plot(range(term-1), python_weeks_smooth_norm, label='python', linewidth=5.0) | |
plt.xticks([30,60,90,120], ['May 2015','November 2015', 'June 2016', 'January 2017'], fontsize=15) | |
plt.legend() | |
plt.gcf().set_size_inches(18.5, 10.5, forward=True) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment