Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. # Time-series impressions (DOW, HOD, etc) (0 = Sunday... 6 = Saturday)
  2. gmt_offset = -4
  3.  
  4. # Create proper datetime column, apply local GMT offset
  5. tweet_df['ts'] = pd.to_datetime(tweet_df['time'])
  6. tweet_df['ts'] = tweet_df.ts + pd.to_timedelta(gmt_offset, unit='h')
  7.  
  8. # Add hour of day and day of week columns
  9. tweet_df['hod'] = [t.hour for t in tweet_df.ts]
  10. tweet_df['dow'] = [t.dayofweek for t in tweet_df.ts]
  11.  
  12. hod_dict = {}
  13. hod_count = {}
  14. dow_dict = {}
  15. dow_count = {}
  16. weekday_dict = {0: 'Mon', 1: 'Tue', 2: 'Wed', 3: 'Thu', 4: 'Fri', 5: 'Sat', 6: 'Sun'}
  17.  
  18. # Process tweets, collect stats
  19. for i in tweet_df.index:
  20. hod = tweet_df.ix[i]['hod']
  21. dow = tweet_df.ix[i]['dow']
  22. imp = tweet_df.ix[i]['impressions']
  23.  
  24. if hod in hod_dict:
  25. hod_dict[hod] += int(imp)
  26. hod_count[hod] += 1
  27. else:
  28. hod_dict[hod] = int(imp)
  29. hod_count[hod] = 1
  30.  
  31. if dow in dow_dict:
  32. dow_dict[dow] += int(imp)
  33. dow_count[dow] += 1
  34. else:
  35. dow_dict[dow] = int(imp)
  36. dow_count[dow] = 1
  37.  
  38. print 'Average impressions per tweet by hour tweeted:'
  39. print '----------------------------------------------'
  40. for hod in hod_dict:
  41. print hod, '-', hod+1, ':', hod_dict[hod]/hod_count[hod], '=>', hod_count[hod], 'tweets'
  42.  
  43. print '\nAverage impressions per tweet by day of week tweeted:'
  44. print '-----------------------------------------------------'
  45. for dow in dow_dict:
  46. print weekday_dict[dow], ':', dow_dict[dow]/dow_count[dow], '=>', dow_count[dow], ' tweets'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement