Skip to content Skip to sidebar Skip to footer

Python Collections.counter

New to python and wondering how can I tell python that the data in row 3 are all one number not individual numbers? I am using collections.Counter but it may not be the correct th

Solution 1:

I think you want

>>>import csv, collections>>>>>>withopen("datafile.csv", "rb") as fp:...    reader = csv.reader(fp)...    counts = collections.Counter(row[3] for row in reader)...>>>counts
Counter({'1234570': 5, '1234567': 3, '1234568': 2, '1234569': 1, '1234577': 1})

Right now, you're not making one Counter object, you're asking for a Counter object for each row, and each instance is counting the characters in row[3].

Post a Comment for "Python Collections.counter"