Hacker News is a site started by the startup incubator Y Combinator, where user-submitted stories (known as "posts") are voted and commented upon, similar to reddit. Hacker News is extremely popular in technology and startup circles.
In this project, we'll compare two different types of posts from Hacker News, those which begin with either Ask HN or Show HN.
We'll specifically compare these two types of posts to determine the following:
*Do Ask HN or Show HN receive more comments on average?
*Do posts created at a certain time receive more comments on average?
from csv import reader
open_file = open('hacker_news.csv')
read_file = reader(open_file)
hacker_news = list(read_file)
hacker_news[:5]
headers = hacker_news[1]
hacker_news = hacker_news[1:]
print(headers)
print(hacker_news[:5])
First, we'll identify posts that begin with either Ask HN or Show HN and separate the data for those two types of posts into different lists.
ask_posts = []
show_posts = []
other_posts = []
for row in hacker_news:
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(len(ask_posts))
print(len(show_posts))
print(len(other_posts))
total_ask_comments = 0
for row in ask_posts:
total_ask_comments+=int(row[4])
avg_ask_comments = total_ask_comments/len(ask_posts)
print(avg_ask_comments)
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)
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 = {}
for row in result_list:
date = row[0]
comment = row[1]
hour = dt.datetime.strptime(date,"%m/%d/%Y %H:%M").strftime('%H')
if hour not in counts_by_hour:
counts_by_hour[hour] = 1
comments_by_hour[hour] = comment
else:
counts_by_hour[hour]+=1
comments_by_hour[hour]+=comment
print(counts_by_hour)
print(comments_by_hour)
avg_by_hour = []
for hr in comments_by_hour:
avg_by_hour.append([hr, comments_by_hour[hr] / counts_by_hour[hr]])
avg_by_hour
swap_avg_by_hour = []
for row in avg_by_hour:
swap_avg_by_hour.append([row[1],row[0]])
print(swap_avg_by_hour)
sorted_swap = sorted(swap_avg_by_hour,reverse=True)
sorted_swap
print('Top 5 Hours for Ask Posts Comments')
for avg,hr in sorted_swap[:5]:
print('{}: {:.2f} average comments per post'.format(dt.datetime.strptime(hr, "%H").strftime("%H:%M"),avg))