Guided Project: Exploring Hacker News Posts, step 3

So, I’m at the point of checking if a title starts with a short phrase, and as the last code shows the way i’ve written that check should work. The answer key even has it formatted the same. But for whatever reason it doesn’t work

import pandas as pd
import numpy as np

hn = pd.read_csv('hacker_news.csv')
print(hn[:5])

output(left in markdown for formatting):

0  12224879                          Interactive Dynamic Video   
1  10975351  How to Use Open Source and Shut the ■■■■ Up at...   
2  11964716  Florida DJs May Face Felony for April Fools' W...   
3  11919867       Technology ventures: From Idea to Enterprise   
4  10301696  Note by Note: The Making of Steinway L1037 (2007)   

                                                 url  num_points  \
0            http://www.interactivedynamicvideo.com/         386   
1  http://hueniverse.com/2016/01/26/how-to-use-op...          39   
2  http://www.thewire.com/entertainment/2013/04/f...           2   
3  https://www.amazon.com/Technology-Ventures-Ent...           3   
4  http://www.nytimes.com/2007/11/07/movies/07ste...           8   

   num_comments      author       created_at  
0            52    ne0phyte   8/4/2016 11:52  
1            10      josep2  1/26/2016 19:30  
2             1    vezycash  6/23/2016 22:20  
3             1     hswarna   6/17/2016 0:01  
4             2  walterbell   9/30/2015 4:12  

The below code is where things stop working as they should.
HERE

ask_posts = list()
# ask hn
show_posts = list()
# show hn
other_posts = list()
# other

for row in hn:
#    title from row[1], make lowercase, if title starts with list name
#    move to relevant list

    title = row[1]
    
    if title.lower().startswith("ask hn"):
        ask_posts.append(title)
        
    elif title.lower().startswith("show hn"):
        show_posts.append(title)
        
    else:
        other_posts.append(title)

print(len(ask_posts))
print(len(show_posts))
print(len(other_posts))

output:
0
0
7
TO HERE

demonstration that the code

if row[1].lower().startswith(‘ask hn’):

should work

print([x.lower().startswith('ask hn') for x in hn.iloc[0:8,1]])

output:
[False, False, False, False, False, False, False, True]

print(hn.iloc[7,1])

output:
Ask HN: How to improve my personal website?

You are loading in the dataset with Pandas. hn is a DataFrame. So, row in hn will be the column names. And so, row[1] will just be the first character of the column name.

It’s not the same as the Solution they provide because they load in the data as a list of lists.

If you want to continue with Pandas, then you need to index the data accordingly as well. A simple way of doing that with minimal modification to your code would be to use iterrows().

So I made changes:

for val in hn.iloc[:,1]:
#    title from row[1], make lowercase, if title starts with list name
#    move to relevant list
    
    if val.lower().startswith("ask hn"):
        ask_posts.append(val)
        
    elif val.lower().startswith("show hn"):
        show_posts.append(val)
        
    else:
        other_posts.append(val)

changed “row” to “val” (readability), and

hn & title = row[1], 

to

 hn.iloc[:,1]