In: Computer Science
Python
I am creating a program that allows a user to extract data from a .csv file and print the statistics of a certain column in that file. The statistics include Count, Mean, Standard Deviation, Min, and Max. Here is the code I have so far:
import csv
import json
class POP:
""" Extract the data """
def __init__(self, line):
self.data = line
# get elements
self.id = self.data[0].strip()
self.geography = self.data[1].strip()
self.targetGeoId = self.data[2].strip()
self.targetGeoId2 = self.data[3].strip()
self.popApr1 = self.data[4].strip()
self.popJul1 = self.data[5].strip()
self.changePop = self.data[6].strip()
population = list()
with open('PopChange.csv', 'r') as p:
reader = csv.reader(p)
next(reader)
for line in reader:
population.append(POP(line))
population.append(POP(line))
obj = POP(line)
print(obj.popApr1)
As seen above, I am trying to print out the Count, Mean, Standard Deviation, Min, and Max of the column "popApr1", which is a column in the file. When I tried to print using print(obj.popApr1), it did not work. I believe that this action can be done using the .describe() method, but I am unsure of the syntax.
Solution:
Code (PLEASE REFER THE SCREENNSHOT FOR INDENTATION OF THE PYTHON SCRIPT):
import csv
""" import statistics module to calculate mean and standard
deviation """
import statistics
class POP:
""" Extract the data """
def __init__(self, line):
self.data = line
# get elements
self.id = self.data[0].strip()
self.geography = self.data[1].strip()
self.targetGeoId = self.data[2].strip()
self.targetGeoId2 = self.data[3].strip()
self.popApr1 = self.data[4].strip()
self.popJul1 = self.data[5].strip()
self.changePop = self.data[6].strip()
population = []
""" list to store popApr1 column value """
popApr1List = []
with open('PopChange.csv', 'r') as p:
reader = csv.reader(p)
next(reader)
for line in reader:
population.append(POP(line))
population.append(POP(line))
obj = POP(line)
""" append the value of obj.popApr1 to list """
popApr1List.append(float(obj.popApr1))
""" print count by getting length of list """
print ("Count: ", len(popApr1List))
""" print mean using statistics module's mean method """
print ("Mean: ", statistics.mean(popApr1List))
""" print min and max using python standard library methods
"""
print("Max: ", max(popApr1List))
print("Min: ", min(popApr1List))
""" print standard deviation using statistics module's stdev
method """
print("Standard Deviation: ", statistics.stdev(popApr1List))
Input file used for testing:
Output: