opened_file = open ('hacker_news.csv')
from csv import reader
read_file = reader(opened_file)
hn = list (read_file)
print(hn[:3])
[['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']]
headers = hn[0]
print(headers)
hn = hn[1:]
print('\n')
print(hn[:3])
['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']]
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row [1]
if title.lower().startswith("ask hn"):
ask_posts.append(row)
elif title.lower().startswith("show hn"):
show_posts.append(row)
else:
other_posts.append(row)
print(ask_posts[:3])
print('\n')
print(show_posts[:3])
print('\n')
print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
[['12296411', 'Ask HN: How to improve my personal website?', '', '2', '6', 'ahmedbaracat', '8/16/2016 9:55'], ['10610020', 'Ask HN: Am I the only one outraged by Twitter shutting down share counts?', '', '28', '29', 'tkfx', '11/22/2015 13:43'], ['11610310', 'Ask HN: Aby recent changes to CSS that broke mobile?', '', '1', '1', 'polskibus', '5/2/2016 10:14']] [['10627194', 'Show HN: Wio Link ESP8266 Based Web of Things Hardware Development Platform', 'https://iot.seeed.cc', '26', '22', 'kfihihc', '11/25/2015 14:03'], ['10646440', 'Show HN: Something pointless I made', 'http://dn.ht/picklecat/', '747', '102', 'dhotson', '11/29/2015 22:46'], ['11590768', 'Show HN: Shanhu.io, a programming playground powered by e8vm', 'https://shanhu.io', '1', '1', 'h8liu', '4/28/2016 18:05']] 1744 1162 17194
total_ask_comments = 0
for row in ask_posts:
num_comments = row[4]
num_comments = int(num_comments)
total_ask_comments += num_comments
print(total_ask_comments)
avg_ask_comments = total_ask_comments/len(ask_posts)
print(round(avg_ask_comments))
total_show_comments = 0
for row in show_posts:
num_comments = row[4]
num_comments = int(num_comments)
total_show_comments += num_comments
print(total_show_comments)
avg_show_comments = total_show_comments/len(show_posts)
print(round(avg_show_comments))
24483 14 11988 10
"Ask" posts receive more comments on avarage than "Show" posts. Possibly , the reason is that it is faster to ask than to show, or "Ask" type questions faster in delievring information than "Show" type.
import datetime as dt
result_list = []
for row in ask_posts:
time = row[6]
num_comments = row[4]
num_comments = int(num_comments)
result_list.append([time, num_comments])
counts_by_hour = {}
comments_by_hour = {}
for row in result_list:
datetime_str = row[0]
datetime_hour = dt.datetime.strptime(datetime_str, "%m/%d/%Y %H:%M")
datetime_h = datetime_hour.strftime("%H")
num_comments = row[1]
if datetime_h not in counts_by_hour:
counts_by_hour[datetime_h] = 1
comments_by_hour[datetime_h] = num_comments
else:
counts_by_hour[datetime_h] += 1
comments_by_hour[datetime_h] += num_comments
print(comments_by_hour)
print('\n')
print(counts_by_hour)
{'09': 251, '13': 1253, '10': 793, '14': 1416, '16': 1814, '23': 543, '12': 687, '17': 1146, '15': 4477, '21': 1745, '20': 1722, '02': 1381, '18': 1439, '03': 421, '05': 464, '19': 1188, '01': 683, '22': 479, '08': 492, '04': 337, '00': 447, '06': 397, '07': 267, '11': 641} {'09': 45, '13': 85, '10': 59, '14': 107, '16': 108, '23': 68, '12': 73, '17': 100, '15': 116, '21': 109, '20': 80, '02': 58, '18': 109, '03': 54, '05': 46, '19': 110, '01': 60, '22': 71, '08': 48, '04': 47, '00': 55, '06': 44, '07': 34, '11': 58}
avg_by_hour = []
for hour in comments_by_hour:
avg_by_hour.append([hour,comments_by_hour[hour]/counts_by_hour[hour]])
print(avg_by_hour[:5])
[['09', 5.5777777777777775], ['13', 14.741176470588234], ['10', 13.440677966101696], ['14', 13.233644859813085], ['16', 16.796296296296298]]
swap_avg_by_hour = []
for row in avg_by_hour:
avg_num = row[1]
hour = row[0]
swap_avg_by_hour.append([avg_num, hour])
print(swap_avg_by_hour[:5])
print('\n')
sorted_swap = swap_avg_by_hour
sorted_swap = sorted(sorted_swap, reverse = True)
print(sorted_swap[:5])
print('\n')
print("Top 5 Hours for Ask Posts Comments")
for row in sorted_swap[:5]:
average = float(row[0])
hours = row[1]
hours_h = dt.datetime.strptime(hours, "%H")
hour = hours_h.strftime('%H:%M')
avg_com_p_post = "{}: {:.2f} average comments per post".format(hour,average)
print(avg_com_p_post)
[[5.5777777777777775, '09'], [14.741176470588234, '13'], [13.440677966101696, '10'], [13.233644859813085, '14'], [16.796296296296298, '16']] [[38.5948275862069, '15'], [23.810344827586206, '02'], [21.525, '20'], [16.796296296296298, '16'], [16.009174311926607, '21']] Top 5 Hours for Ask Posts Comments 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
As we see above the most active hours among commentators are two hours in the afternoon ( from 15:00 till 17:00) and in the evening (from 20:00 till 22:00), when people have some free time for the breake or rest. BUt, the time in our dataset is Eastern US, and we should remember about time difference in 8 hours with our local time