Python Forum
Hi how to take row 1 for every column and check the value what class is it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hi how to take row 1 for every column and check the value what class is it
#1
Hi I have homework and i am stuck as to how to take row 1 for every column in the given csv file and check the value inside the cell what class is it? if it is numeric? and to count the total unique value in every column. This is what i have so far. Appreciate your help.

with open(hdbrpi) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(row[line_count], "<class 'str'>" , "isnumeric:", row[line_count].isnumeric())
            line_count += 1
Reply
#2
First, clarify the row one. I expect that means the first data point. But if the csv file has headers, that's going to be on the second row (row[1] with 0-indexing).

I would not trust isnumeric unless you have been told it's okay to do so for this exercise. '-1' and '3.5' give False for isnumeric. Do you need to distinguish between ints and floats? If not you can just use float() with a try/except block, otherwise you'll have to test both. Has your teacher covered try/except blocks yet?

When you get to the right row to check, you need to have a subloop to loop though the fields in the row (for field in row:). Then check each field for numeric or string.

To get unique values use set(). Start with a list of one set for every column. Then have another subloop just like the one above to add each item to the correct set:

for column, item in enumerate(row):
    unique[column].add(item)
After reading the whole file, the len of each set will be the number of items in that column.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
ichabod801 thanks for your help, i will work on your input and figure it out thanks so much.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020