Why my code automatically rounded the numbers?

Hi! When I did the guided project: Hacker News, I tried to calculate the average numbers of comments for different hours. However the results are all integer. I didn’t round my number in my code, so I don’t know why it happened. Because my dataset seems a little different than the data set used in the solution, I’m not sure if my answer is right either.
Here is the document: Exploring Hacker News Posts.ipynb (9.3 KB)

Here is my code in that line:

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

Here is the output:
[[‘02’, 11],
[‘03’, 7],
[‘00’, 7],
[‘01’, 7],
[‘20’, 8],
[‘21’, 8],
[‘22’, 8],
[‘23’, 6],
[‘08’, 9],
[‘09’, 6],
[‘14’, 9],
[‘06’, 6],
[‘07’, 7],
[‘11’, 8],
[‘10’, 10],
[‘13’, 16],
[‘12’, 12],
[‘15’, 28],
[‘04’, 9],
[‘17’, 9],
[‘16’, 7],
[‘19’, 7],
[‘18’, 7],
[‘05’, 8]]

Click here to view the jupyter notebook file in a new tab

The dataset used in the Dataquest platform has been altered:

You can find the data set here, but note that it has been reduced from almost 300,000 rows to approximately 20,000 rows by removing all submissions that did not receive any comments, and then randomly sampling from the remaining submissions.

So you’re going to have different numbers in your results because you’re using the original dataset, compared to the solution notebook.

Are you using Python 2? This would account for all your results being integers. In Python 2, when you divide with integers, you get an integer result. You can read more about it in this article. I ran your code on my machine with Python 3, and I get results with floats:

[['02', 11.137546468401487],
 ['01', 7.407801418439717],
 ['22', 8.804177545691905],
 ['21', 8.687258687258687],
 ['19', 7.163043478260869],
 ['17', 9.449744463373083],
 ['15', 28.676470588235293],
 ['14', 9.692007797270955],
 ['13', 16.31756756756757],
 ['11', 8.96474358974359],
 ['10', 10.684397163120567],
 ['09', 6.653153153153153],
 ['07', 7.013274336283186],
 ['03', 7.948339483394834],
 ['23', 6.696793002915452],
 ['20', 8.749019607843136],
 ['16', 7.713298791018998],
 ['08', 9.190661478599221],
 ['00', 7.5647840531561465],
 ['18', 7.94299674267101],
 ['12', 12.380116959064328],
 ['04', 9.7119341563786],
 ['06', 6.782051282051282],
 ['05', 8.794258373205741]]
1 Like