1
#!/bin/python3
import csv
import statistics
def read_cvs():
        with open('hw_25000.csv', 'r') as csv_rf:
                cvs_reader = csv.DictReader(csv_rf)
                for line in cvs_reader:
                        print(line[' "Height(Inches)"'])
read_cvs()

I have this code that reads my file and prints out my height values but I am not sure how to print out the most frequent height values with statistics.mode().

The CSV file is available in https://people.sc.fsu.edu/~jburkardt/data/csv/csv.html

Manu mathew
  • 859
  • 8
  • 25
  • You read the file and immediately discard its content. You may want to look into [this](https://stackoverflow.com/questions/24662571/python-import-csv-to-list) example first. – DYZ Apr 02 '22 at 03:42

2 Answers2

1

#Try this

print(statistics.mode(line[' "Height(Inches)"']))
pyaj
  • 545
  • 5
  • 15
Kunj Jani
  • 9
  • 1
0

The header in that file contains an extra space before the text for each column name. This can be removed by using the skipinitialspace=True option. Also the CSV reader will read everything in as a string so the values will need converting to a float.

Try the following approach:

import csv
import statistics

def read_cvs():
    heights = []
    
    with open('hw_25000.csv', 'r') as csv_rf:
        cvs_reader = csv.DictReader(csv_rf, skipinitialspace=True)
        
        for line in cvs_reader:
            heights.append(float(line['Height(Inches)']))

    print(statistics.mode(heights))
    
read_cvs()    

For your example CSV file this gives:

70.04724


A shorter version would be:

def read_cvs():
    with open('hw_25000.csv', 'r') as csv_rf:
        cvs_reader = csv.DictReader(csv_rf, skipinitialspace=True)
        print(statistics.mode(float(line['Height(Inches)']) for line in cvs_reader))
Martin Evans
  • 45,791
  • 17
  • 81
  • 97