#!/usr/bin/env python # coding: utf-8 # # Hacker News Posts # # In this project we will be looking at two different types of posts: `Ask HN` and `Show HN`. # # `Ask HN` is where users submit posts to ask the Hacker NEws community questions. # # `Show HN` is where users submits posts to showcase Hacker NEws a project, product or something interesting that was found. # # In this project we will be specifically looking at: # * which type of post receives more comments on average # * are posts created at a certain time receving more comments on average. # # It is important to note that the dataset being worked with has been significantly reduced from 300,000 rows to 20,000 due to removal of submittions without comments and randomly sampling the remaining submissions. # In[1]: import csv opened_file = open('hacker_news.csv') hn = list(csv.reader(opened_file)) hn[:5] # In[2]: headers = hn[0] hn = hn[1:] print(headers) print(hn[:5]) # In[3]: 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(len(ask_posts)) print(len(show_posts)) print(len(other_posts)) # In[4]: 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) # On average, ask posts receive approximately 14 comments whereas show posts receive 10 comments. As ask posts receive more comments, we will focus on this going forward. # In[5]: import datetime as dt result_list = [] for row in ask_posts: result_list.append([row[6], int(row[4])]) comments_by_hour = {} counts_by_hour = {} date_format = "%m/%d/%Y %H:%M" for row in result_list: date = row[0] comment = row[1] time = dt.datetime.strptime(date, date_format).strftime("%H") if time not in counts_by_hour: comments_by_hour[time] = comment counts_by_hour[time] = 1 else: comments_by_hour[time] += comment counts_by_hour[time] += 1 comments_by_hour # In[6]: avg_by_hour = [] for hour in comments_by_hour: avg_by_hour.append([hour, comments_by_hour[hour] / counts_by_hour[hour]]) avg_by_hour # In[7]: 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 # In[8]: print("Top 5 Hours for Ask Posts Comments") for avg, hour in sorted_swap[:5]: print("{}: {:.2f} average comments per post".format(dt.datetime.strptime(hour, "%H").strftime("%H:%M"), avg)) # # Conclusion # # In this project, we looked at both ask and show posts in order to find which type of post received the most comments on average. From this, we wanted to find, what time would receive the most comments on average. # # From our analusis, we see that an ask post should be created around 15:00 - 16:00 to maximize the amount of comments received. # # It must be noted this data excluded the analysis of posts with 0 comments. It is accurate to state that the posts that had received comments, ask posts received more comments on average and those posts created between 15:00 - 16:00 received the most comments on average # In[ ]: