Home > Blockchain >  Dictionary from web scv file in Python
Dictionary from web scv file in Python

Time:03-14

I try to return a dictionary,that contains 3 different values. (The total number of sales per Branch and per Customer Type)

I need to return dictinary like

{"A": {"Member": 230, "Normal": 351}, 
"B": {"Member": 123, "Normal": 117}, 
"C": {"Member": 335, "Normal": 18}} 

What am I doing wrong, can someone explain me, please ?

import csv
dict_from_csv = {} 
file = open('supermarket_sales.csv')
csv_reader = csv.reader(file)
next(csv_reader)
for row in csv_reader:

    branch= row[1]
    Customer = row[3]
    total = float(row[9])
    curent_total= dict_from_csv.get(branch)
    if dict_from_csv.get(branch) is None:

        dict_from_csv[branch]={}  

    else:
        if dict_from_csv[branch].get(Customer) is None:
            dict_from_csv[branch]={Customer: 0} 
        else :
            curent_total= dict_from_csv.get(branch)  
       
print(dict_from_csv)

I can return only:

{'A': {'Member': 0}, 
'C': {'Normal': 0}, 
'B': {'Normal': 0}}

scv file: https://app.box.com/s/f4hcfkferizntbev3ou8hso6nyfccf74

CodePudding user response:

First mistake:

dict_from_csv[branch]={Customer: 0}

it creates new dictionary with new Customer but it also removes previous dictionary with prediction customer.

It should be

dict_from_csv[branch][Customer] = 0

and it adds new customer to existing dictionary.


Second mistake: you don't have = 1 to count elements

dict_from_csv[branch][customer]  = 1

Full working code:

import csv

f = open('supermarket_sales.csv')
csv_reader = csv.reader(f)
next(csv_reader)

dict_from_csv = {}

for row in csv_reader:
    branch= row[1]
    customer = row[3]

    if branch not in dict_from_csv:
        dict_from_csv[branch] = {}  

    if customer not in dict_from_csv[branch]:
        dict_from_csv[branch][customer] = 0
        
    dict_from_csv[branch][customer]  = 1
       
print(dict_from_csv)

Result:

{
'A': {'Member': 167, 'Normal': 173}, 
'C': {'Normal': 159, 'Member': 169}, 
'B': {'Member': 165, 'Normal': 167}
}

BTW: if you would use pandas.DataFrame then using

import pandas as pd

df = pd.read_csv('/home/furas/supermarket_sales.csv')

sizes = df.groupby(['Branch','Customer type']).size()

print(sizes)

you would get

Branch  Customer type
A       Member           167
        Normal           173
B       Member           165
        Normal           167
C       Member           169
        Normal           159
dtype: int64

Bigger problem makes to convert it to expected dictionary.
There is .to_dict() and .to_json() but they don't create expected dictionary.

But this works for me:

result = dict()

for ((brand, customer), number) in sizes.items():
    if brand not in result:
        result[brand] = dict()
    result[brand][customer] = number

print(result)
  • Related