title: adding a count next to an aggregated data in python
body: <p>I have data in a csv that needs to be parsed. It looks like:</p>
  
  <pre><code>Date,Tag,Amount
  13/06/2018,ABC,6750000
  13/06/2018,ABC,159800
  24/05/2018,ABC,-1848920
  16/05/2018,AB,-1829700
  16/05/2018,AB,3600000
  28/06/2018,A,15938000
  16/05/2018,AB,3748998
  28/06/2018,A,1035000
  28/06/2018,A,1035000
  14/06/2018,ABC,2122717
  </code></pre>
  
  <p>what I am trying to achieve is to make the date and tag the key and group by the date and tag and to sum up the amount. this is working for me, however, I want to show the count next to each record to show how many data set has been put together.</p>
  
  <p>expected result</p>
  
  <pre><code>Date,Tag,Amount,Count
  13/06/2018,ABC,6909800,2
  24/05/2018,ABC,-1848920,1
  16/05/2018,AB,5519298,3
  28/06/2018,A,18008000,3
  14/06/2018,ABC,2122717,1
  </code></pre>
  
  <p>the code I am using now is grouping by the date and tag and summing up the amount but it does not show the count.</p>
  
  <pre><code>from collections import defaultdict
  import csv
  
  d = defaultdict(lambda: defaultdict(int))
  
  for line in z:
      tokens = [t.strip() for t in line.split(",")]
      try:
          date = tokens[0]
          tag = tokens[1]
          amount = int(tokens[2])
      except ValueError as e:
          continue
      d[date][tag] += amount
  
  s = '\n'.join(['{0} {1} {2}'.format(k, k2, v2) for k,v in d.items() for k2,v2 in v.items()])
  
  with open('output.txt', 'w') as f:
          f.write(s)
  </code></pre>
  
  <p>could someone show me how to achieve this please without using pandas</p>
  
  <p>thanks</p>