import csv import matplotlib.pyplot as plt def csv_rows_to_label_count(rows, column): """ take a list of csv rows and for a given column return a dictionary of labels mapped to the number of times that label appears """ results = {} # loop through rows # if we are seeing the label for the first time set label to 1 # otherwise increment that label's current count by 1 for row in rows: val = row[column] if val == "": val = "Unknown" if results.has_key(val): results[val] = results[val] + 1 else: results[val] = 1 return results with open("Whorls.csv") as f: reader = csv.DictReader(f) results = csv_rows_to_label_count(reader, 'Structure') for key in results: print("%s:%s" % (key, results[key]))