#!/usr/bin/env python # coding: utf-8 # # # Hacker News Posts # In this project, we'll work with a data set of submissions to popular technology site Hacker News [https://news.ycombinator.com/]. # # 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, and posts that make it to the top of Hacker News' listings can get hundreds of thousands of visitors as a result. # # You can find the data set here [https://www.kaggle.com/hacker-news/hacker-news-posts] # # Below are descriptions of the columns: # # *id:* The unique identifier from Hacker News for the post. # # *title:* The title of the post. # # *url:* The URL that the posts links to, if it the post has a URL. # # *num_points:* The number of points the post acquired, calculated as the total number of upvotes minus the total number of downvotes. # # *num_comments:* The number of comments that were made on the post. # # *author:* The username of the person who submitted the post. # # *created_at:* The date and time at which the post was submitted. # # In[1]: from csv import* open_file=open("hacker_news.csv") read_file=reader(open_file) hn=list(read_file) print(hn[0:3]) # In[2]: header=hn[0] hn=hn[1:] print(header) print(hn[0]) # In[3]: ask_posts=[] show_posts=[] other_posts=[] for row in hn: title=row[1] 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),len(show_posts),len(other_posts)) # Here we seperated posts into *Ask posts*, *Show posts*,and *Other posts* based on beginning of title. # In[4]: total_ask_comments=0 for comment in ask_posts: element=int(comment[4]) total_ask_comments+=element avg_ask_comments=total_ask_comments/len(ask_posts) print("Average Ask Comments : ",avg_ask_comments) total_show_comments=0 for comment in show_posts: element=int(comment[4]) total_show_comments+=element avg_show_comments=total_show_comments/len(show_posts) print("Average Show Comments : ",avg_show_comments) # Here we calculated average of ask and show comments. # On an average Ask post received more comments. # # Since Ask posts are more likely to receive comments, we'll focus our remaining analysis just on these posts. # # Now we will calculate number of Ask post created in each hour of # day, along with number of comments received per hour. # In[5]: import datetime as dt result_list=[] for element in ask_posts: created_at=element[6] comment=int(element[4]) result_list.append([created_at, comment]) print(len(result_list)) print(result_list[:5]) counts_by_hour={} comments_by_hour={} for element in result_list: date_hour=element[0] comment=element[1] date_hour=dt.datetime.strptime(date_hour,"%m/%d/%Y %H:%M") hour=dt.datetime.strftime(date_hour,"%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) # we started by initiating empty list *result_list* # # loop over *ask_posts* and # append date-time and comment corresponding to each post in *result_list*, now *result_list* is nested list, each list with two elements. # # Create two empty dictionaries # *counts_by_hour* : for number of posts per hour # *comment_by_hour* : for number of comments per hour # # Then loop through each row of *result_list* # assign datetime to variable *date_hour* # and comment to variable *comment* # # As we have to calculate number of posts and comments for each hour, we used **if** statement and filled above two dictionaries # # # # # # # In[6]: 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) # we calculated the average number of comments for posts created during each hour of the day, and stored the results in a list of lists *named avg_by_hour* # In[7]: swap_avg_by_hour=[] for i in avg_by_hour: x=i[0] y=i[1] swap_avg_by_hour.append([y,x]) print(swap_avg_by_hour) # In[8]: sorted_swap=sorted(swap_avg_by_hour, reverse=True) for i in sorted_swap[:5]: hour=i[1] avg=i[0] hour=dt.datetime.strptime(hour,"%H") top5="{} : {:.2f} average comments per post.".format(hour,avg) print(top5) # Here, we sorted *swap_avg_by_hour* in descending order. And then printed top 5 hours for ask posts comments. # The most comments on post are between 15:00 UTC i.e, 3pm to 2:00 UTC (2 am)