18. Counting the most frequent names (PP)

Hello sir/ maam. I got the original purpose of this practice problem, that there are 6 names which appear more than or equal to 100000, but however what if i want to print those names, i tried this code but its not working, i tried solving it but i am not successful. It would be great if someone helps. Thanks a lot

Screen Link:

My Code:

import csv
file = open('dq_unisex_names.csv')
reader = csv.reader(file)
rows = list(reader)

names = []
for row in rows[1:]:
    name = str(row[0])
    if name >= 100000:
        names.append(name)
        
print(names)

What I expected to happen: To get the names of the 6 people who are >=100000 times in the file.

What actually happened:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-c1d47aaf23cc> in <module>
      7 for row in rows[1:]:
      8     name = str(row[0])
----> 9     if name >= 100000:
     10         names.append(name)
     11 

TypeError: '>=' not supported between instances of 'str' and 'int'

Hi @uditchauhan,

But you should compare with 100000 the second column (estimated_number), not the first one (name). Before, you have actually to convert it to float for such a comparison, since now it’s in str.
And in this line of your code
if name >= 100000:
you are trying to compare a string with a float.

If you want to create a list of those names:

names = []
for row in rows[1:]:
    name = row[0]
    if float(row[1]) >= 100000:
        names.append(name)       
print(names)
2 Likes

oh yes im silly why did i changed the row. Thank you for the quick reply. Have a good day/night.

1 Like