Home > Back-end >  How to read a CSV file and extract the most frequent value in it using statistics.model() in Python?
How to read a CSV file and extract the most frequent value in it using statistics.model() in Python?

Time:04-03

#!/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

CodePudding user response:

#Try this

print(statistics.mode(line[' "Height(Inches)"']))

CodePudding user response:

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))
  • Related