2bam

Calc de %positivos por testeados en CABA para dataset gobBsAs

Apr 7th, 2021 (edited)
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. import csv
  2. from collections import defaultdict, Counter
  3. from time import time
  4. import re
  5. from datetime import datetime
  6.  
  7. por_fecha = defaultdict(Counter)
  8. RE_FECHA = re.compile(r'[0-9]+([A-Z]+[0-9]+)\:.*')
  9.  
  10. with open('casos_covid19.csv', newline='', encoding='utf-8-sig') as csvfile:
  11.     reader = csv.DictReader(csvfile)
  12.     i = 0
  13.     start = time()
  14.     for row in reader:
  15.         fecha_raw = row['fecha_toma_muestra'] or row['fecha_clasificacion']
  16.         fecha = RE_FECHA.match(fecha_raw).group(1)
  17.         por_fecha[fecha][row['clasificacion']] += 1
  18.         i+=1
  19.         if i % 10000 == 0:
  20.             print('.', end='')
  21.     end = time()
  22.  
  23. print()
  24. print(f'Tomó {end-start:.3} segundos para {i} registros')
  25.  
  26. print(f"{'fecha':^7} {'%+':^6} {'total':^6} -- detalle")
  27. for fecha, cont in sorted(por_fecha.items(), key=lambda kv: datetime.strptime(kv[0], '%b%Y')):
  28.     total = sum(cont.values())
  29.     cont_str = ''.join(f'{k:>11}:{v:<6}' for k, v in sorted(cont.items(), key=lambda kv: kv[0]))
  30.     print(f"{fecha:7} {cont['confirmado']/total:6.1%} {total:6} -- {cont_str}")
  31.  
Add Comment
Please, Sign In to add comment