I am attempting to calculate all variables of a specific value in a given column from an Excel document. I want to be able to iterate over the column and calculate the total of each instance... e.g. how many students received a grade "A".
Here is what I have so far...
test.xls:
Name, Class, Grade
James, Math, A
Judy, Math, A
Bill, Social Studies, B
Denice, History, C
Sarah, History, B
Here is my python script
import xlrd
from collections import Counter
sh = xlrd.open_workbook('test.xls', on_demand = True).sheet_by_index(0) # Open workbook and sheet
for rownum in range(sh.nrows):
grades = str(sh.cell(rownum, 2).value) # Grab all variables in column 2.
print Counter(grades.split('\n')) # Count grades
Expected output:
A = 2
B = 2
C = 1
Actual output:
Counter({'Grade': 1})
Counter({'A': 1})
Counter({'A': 1})
Counter({'B': 1})
Counter({'C': 1})
Counter({'B': 1})
As each grade is showing in a different list I have been unable to merge/concatenate lists to get a total. Also it is not in the desired output formatting.