opened_file = open('hacker_news.csv')
import csv
read_file = csv.reader(opened_file)
hn=list(read_file)
hn[0:5]
[['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at'], ['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01']]
header=hn[0]
header
['id', 'title', 'url', 'num_points', 'num_comments', 'author', 'created_at']
hn=hn[1:]
hn[0:5]
[['12224879', 'Interactive Dynamic Video', 'http://www.interactivedynamicvideo.com/', '386', '52', 'ne0phyte', '8/4/2016 11:52'], ['10975351', 'How to Use Open Source and Shut the Fuck Up at the Same Time', 'http://hueniverse.com/2016/01/26/how-to-use-open-source-and-shut-the-fuck-up-at-the-same-time/', '39', '10', 'josep2', '1/26/2016 19:30'], ['11964716', "Florida DJs May Face Felony for April Fools' Water Joke", 'http://www.thewire.com/entertainment/2013/04/florida-djs-april-fools-water-joke/63798/', '2', '1', 'vezycash', '6/23/2016 22:20'], ['11919867', 'Technology ventures: From Idea to Enterprise', 'https://www.amazon.com/Technology-Ventures-Enterprise-Thomas-Byers/dp/0073523429', '3', '1', 'hswarna', '6/17/2016 0:01'], ['10301696', 'Note by Note: The Making of Steinway L1037 (2007)', 'http://www.nytimes.com/2007/11/07/movies/07stein.html?_r=0', '8', '2', 'walterbell', '9/30/2015 4:12']]
ask_posts=[]
show_posts=[]
other_posts=[]
for row in hn:
title = row[1]
title =title.lower()
if title.startswith('ask hn'):
ask_posts.append(row)
elif title.startswith('show hn'):
show_posts.append(row)
else :
other_posts.append(row)
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
1744 1162 17194
total_ask_comments = 0
for row in ask_posts:
total_ask_comments+= int(row[4])
total_ask_comments
24483
avg_ask_comments = total_ask_comments/len(ask_posts)
print(avg_ask_comments)
14.038417431192661
total_show_comments = 0
for row in show_posts:
total_show_comments+= int(row[4])
avg_show_comments = total_show_comments/len(show_posts)
print(avg_show_comments)
10.31669535283993
ask posts receive more comment then show posts
import datetime as dt
result_list = []
for row in ask_posts:
result_list.append([row[6],int(row[4])])
counts_by_hour = {}
comments_by_hour={}
result_list[0][0]
'8/16/2016 9:55'
date = dt.datetime.strptime(result_list[0][0], "%m/%d/%Y %H:%M")
date
datetime.datetime(2016, 8, 16, 9, 55)
hour = dt.date.strftime(date,"%H")
hour
'09'
for row in result_list:
date = row[0]
date = dt.datetime.strptime(date, "%m/%d/%Y %H:%M")
hour = dt.date.strftime(date,"%H")
if hour not in counts_by_hour:
counts_by_hour[hour] = 1
comments_by_hour[hour] = row[1]
else:
counts_by_hour[hour] += 1
comments_by_hour[hour] = comments_by_hour[hour]+row[1]
from dateutil import parser
temp = parser.parse(result_list[0][0])
temp
datetime.datetime(2016, 8, 16, 9, 55)
temp = temp.strftime("%H")
temp
'09'
counts_by_hour
{'00': 55, '01': 60, '02': 58, '03': 54, '04': 47, '05': 46, '06': 44, '07': 34, '08': 48, '09': 45, '10': 59, '11': 58, '12': 73, '13': 85, '14': 107, '15': 116, '16': 108, '17': 100, '18': 109, '19': 110, '20': 80, '21': 109, '22': 71, '23': 68}
comments_by_hour
{'00': 447, '01': 683, '02': 1381, '03': 421, '04': 337, '05': 464, '06': 397, '07': 267, '08': 492, '09': 251, '10': 793, '11': 641, '12': 687, '13': 1253, '14': 1416, '15': 4477, '16': 1814, '17': 1146, '18': 1439, '19': 1188, '20': 1722, '21': 1745, '22': 479, '23': 543}
avg_by_hour = []
for row in counts_by_hour:
avg_by_hour.append([row,comments_by_hour[row]/counts_by_hour[row]])
avg_by_hour
[['21', 16.009174311926607], ['13', 14.741176470588234], ['20', 21.525], ['22', 6.746478873239437], ['23', 7.985294117647059], ['00', 8.127272727272727], ['04', 7.170212765957447], ['14', 13.233644859813085], ['12', 9.41095890410959], ['17', 11.46], ['15', 38.5948275862069], ['01', 11.383333333333333], ['18', 13.20183486238532], ['10', 13.440677966101696], ['16', 16.796296296296298], ['03', 7.796296296296297], ['07', 7.852941176470588], ['08', 10.25], ['11', 11.051724137931034], ['05', 10.08695652173913], ['19', 10.8], ['06', 9.022727272727273], ['02', 23.810344827586206], ['09', 5.5777777777777775]]
swap_avg_by_hour = []
for row in avg_by_hour:
swap_avg_by_hour.append([row[1],row[0]])
print(swap_avg_by_hour)
[[16.009174311926607, '21'], [14.741176470588234, '13'], [21.525, '20'], [6.746478873239437, '22'], [7.985294117647059, '23'], [8.127272727272727, '00'], [7.170212765957447, '04'], [13.233644859813085, '14'], [9.41095890410959, '12'], [11.46, '17'], [38.5948275862069, '15'], [11.383333333333333, '01'], [13.20183486238532, '18'], [13.440677966101696, '10'], [16.796296296296298, '16'], [7.796296296296297, '03'], [7.852941176470588, '07'], [10.25, '08'], [11.051724137931034, '11'], [10.08695652173913, '05'], [10.8, '19'], [9.022727272727273, '06'], [23.810344827586206, '02'], [5.5777777777777775, '09']]
sorted_swap = sorted(swap_avg_by_hour,reverse= True)
print('Top 5 Hours for Ask Posts Comments')
template = "{hour}: {count:.2f} average comments per post"
Top 5 Hours for Ask Posts Comments
for row in sorted_swap[:5]:
date = row[1]
date = dt.datetime.strptime(date, "%H")
hour = date.strftime("%H:%M")
output= template.format(hour=hour,count=row[0])
print(output)
15:00: 38.59 average comments per post 02:00: 23.81 average comments per post 20:00: 21.52 average comments per post 16:00: 16.80 average comments per post 21:00: 16.01 average comments per post
So it can be said that creating a post at 13:00 hour or 1 in the noon in my time region gets more average comment than any other time in the day