Group name in same group

1

I have a sales table with sellers name, products, and other information imported from a xlsx worksheet.

But it turns out that the names of sellers are repeated according to sales, for example:

João  - Venda de produto A 
Marco - Venda de produto B
João - Venda de produto B
João - Venda de produto A

In the example above, I wanted to add up all sales of "John" to compare with those of "Marco" for example, and not that the graphic repeated his name ad infinitum . p>     

asked by anonymous 25.01.2018 / 17:09

2 answers

2

Assuming you have a list of tuples of type:

[ ('JOAO', 10.50), ( 'JOAO', 33.10 ), ('MARCO', 4.00 ), ('JOAO', 15.99), ('MARCO', 21.54) ]

What about:

vendas = [ ('JOAO', 10.50), ( 'JOAO', 33.10 ), ('MARCO', 4.00 ), ('JOAO', 15.99), ('MARCO', 21.54) ]

totais = {}

for vendedor, venda in vendas:
    if vendedor in totais:
        totais[vendedor] += venda
    else:
        totais[vendedor] = venda

print(totais)

Output:

{'MARCO': 25.54, 'JOAO': 59.59}

You can also use defaultdict to simplify the solution:

from collections import defaultdict

vendas = [ ('JOAO', 10.50), ( 'JOAO', 33.10 ), ('MARCO', 4.00 ), ('JOAO', 15.99), ('MARCO', 21.54) ];

totais = defaultdict(float)

for vendedor, venda in vendas:
    totais[vendedor] += venda

print(dict(totais))

Output:

{'MARCO': 25.54, 'JOAO': 59.59}
    
25.01.2018 / 17:35
1

To read the xlsx file you can use the openpyxl library. It can be installed using pip:

pip install openpyxl

Based on Lacobus's response, the script would look like this:

from openpyxl import load_workbook
wb = load_workbook(filename='nome_do_arquivo.xlsx')

totais = {}

for row in wb['nome_da_planilha']:
    vendedor = row[0].value
    if vendedor in totais:
        totais[vendedor] += 1
    else:
        totais[vendedor] = 1

print(totais)
    
25.01.2018 / 20:30