Bottom Page

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
Quote
#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

Quote
#3
ichabod801 thanks for your help, i will work on your input and figure it out thanks so much.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  sending arduino data to python and use a parity check 10_Lucas 6 264 Aug-03-2019, 10:37 AM
Last Post: DeaD_EyE
  Please check code Evgeniy2019 4 281 Apr-05-2019, 06:37 PM
Last Post: Evgeniy2019
  CSV file column swapping DJPunk 5 333 Mar-04-2019, 04:11 AM
Last Post: DJPunk
  How to check an array exist in a file using Python fitrisibarani 17 760 Feb-27-2019, 04:16 PM
Last Post: ichabod801
  Check if string is uppercase or lowercase and eliminate Wolfpack2605 1 1,536 Jan-01-2018, 05:03 AM
Last Post: Mekire
  Check if integer is between two values Wolfpack2605 3 14,941 Dec-24-2017, 06:39 AM
Last Post: DeaD_EyE
  Iterating over pandas.df to check for values out of range Padowan 14 4,674 Nov-26-2017, 04:37 PM
Last Post: heiner55
  check conenction takes byte or not viens 1 807 Nov-01-2017, 06:45 PM
Last Post: nilamo
  "Call the subroutine in order to check..." misa_san 1 1,032 Sep-24-2017, 09:30 AM
Last Post: stranac
  How can I check out my localhost? Mike Ru 2 1,258 Aug-05-2017, 05:50 PM
Last Post: Mike Ru

Forum Jump:


Users browsing this thread: 1 Guest(s)