-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
50 lines (42 loc) · 1.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pandas as pd
import numpy as np
import sys
def process(filename, field, only):
data = pd.read_csv(filename)
def getIDKey():
for f in [f'Número {field.lower()}', field]:
if f in data.keys():
return f
if only:
[f, value] = only.split('=')
only_value = data[f]==value
data = data[only_value]
filtred = data.groupby(field).agg(
{
'Inscritos': 'sum',
'CC': 'sum',
'FPV': 'sum',
'MTS': 'sum',
'UCS': 'sum',
'MAS - IPSP': 'sum',
'21F': 'sum',
'PDC': 'sum',
'MNR': 'sum',
'PAN-BOL': 'sum',
'Votos Válidos': 'sum',
'Blancos': 'sum',
'Nulos': 'sum',
}
)
filtred.to_csv(sys.stdout)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='filter CSV by field')
parser.add_argument('filename', metavar='file_name',
type=str, nargs='+', help='CSV source filenames')
parser.add_argument('--field', default='Municipio', type=str, help='Field to agregate on')
parser.add_argument('--only', default=None, type=str, help='filter fields')
args = parser.parse_args()
for f in args.filename:
if f == '-': f = sys.stdin
process(f, args.field, args.only)