Reputation: 4435
I am trying to loop through column in an access table using Python. The column I have is sorted ascending.
I am trying to loop though the rows and when the value in the column changes, I want to get count of of all those values. In the example column below, the first group of values I want to count are the M1
's. When the next row changes to M21
, I want to count the M21
's until it changes to M23b
and so on.
I don't want to use an if
/else
statement, because there are a few hundred different possible values. I played around with the groupby
function in the itertools
module, but I haven't been able to solve the syntax to work in my example. I also tried a silly loop doing something like if row != row.next(): do_something
but that blew up in my face. If anyone can suggest a work-around or show me a sample script that will do this for me, I'd appreciate it.
Example Column:
M1 M1 M1 M21 M21 M23b M23b S2 S23b S23B O1 O2 O2 O2
Upvotes: 2
Views: 6124
Reputation: 69240
If you want to add printing in a loop doing other work, the following may be helpful:
from collections import Counter # or defaultdict
col_counts = Counter() # or defaultdict(int)
last_value = object() # won't show up in table
for row in access_table:
col_counts[row[field]] += 1
if row[field] != last_value:
print(col_counts[last_value])
last_value = row[field]
...
other_processing()
...
Upvotes: 1
Reputation: 176950
Your intuition to use itertools.groupby
was correct:
for key, group in groupby(column):
count = sum(1 for item in group) # Thanks JBernardo
# the key is what is in the column, count is the number of items
Alternatively, if all you need is the counts, it's as simple as:
from collections import Counter # Python 2.7+
group_counts = Counter(column)
You can implement Counter
as:
from collections import defaultdict:
group_counts = defaultdict(int)
for item in column:
group_counts[item] += 1
on older versions of Python.
Upvotes: 4