Count males and females separately in a nested dict format from csv file

Viewed 89

This code worked fine and it prints result in this format.

enter image description here

I need results in a nested dict format like this.

data = {

           'year': {
                    'male': {'Q1': 1, 'Q2': 1, 'Q3': 1, 'Q4': 1, },
                    'female': { 'Q1': 1, 'Q2': 1, 'Q3': 1, 'Q4': 1, }
                   }
       }

The code:

import csv

results = {'males': {}, 'females': {}}

with open('1000 Records.csv') as csv_file:

    csv_reader = csv.reader(csv_file)

    for row in csv_reader:
        year_of_joining = int(row[17])
        quarter_of_joining = row[15]
        gender = 'males' if row[5] == 'M' else 'females'

        if year_of_joining not in results[gender]:
            results[gender][year_of_joining] = {f'Q{i + 1}': 0 for i in range(4)}
        results[gender][year_of_joining][quarter_of_joining] += 1

years = list(results['males'].keys()) + list(results['females'].keys())
years = sorted(list(set(years)))

for year in years:
    count = [results['males'].get(year, 0), results['females'].get(year, 0)]
    print("Male's and Female's: %s: %s" % (year, count))
3 Answers

You are close. Outside of your for year in years keep a dictionary that stores the running results of yearly counts:

data = {}
for year in years:
    data[year] = {'male':results['males'].get(year, 0), 
                 'female':results['females'].get(year, 0)}

This is working solution:

import csv
import collections

data= {}

with open('1000 Records.csv') as csv_file:

    csv_reader = csv.reader(csv_file)

    for row in csv_reader:
        year_of_joining = int(row[17])
        quarter_of_joining = row[15]
        gender = 'male' if row[5] == 'M' else 'female'

        if year_of_joining not in data:
            data[year_of_joining]={'male': {f'Q{i + 1}': 0 for i in range(4)}, 'female': {f'Q{i + 1}': 0 for i in range(4)}}
        data[year_of_joining][gender][quarter_of_joining] += 1

data = collections.OrderedDict(sorted(data.items())) # sorting

for year in data:
    print("Male's and Female's: %s: %s" % (year, data[year]))

The only difference in code above is that it gives output in slightly different format, but I suspect it may be what you wanted in the first place:

Male's and Female's: 1993: {'male': {'Q1': 0, 'Q2': 0, 'Q3': 0, 'Q4': 1}, 'female': {'Q1': 0, 'Q2': 0, 'Q3': 0, 
'Q4': 0}}
Male's and Female's: 1998: {'male': {'Q1': 0, 'Q2': 0, 'Q3': 0, 'Q4': 1}, 'female': {'Q1': 0, 'Q2': 0, 'Q3': 0, 
'Q4': 0}}
Male's and Female's: 1999: {'male': {'Q1': 0, 'Q2': 1, 'Q3': 1, 'Q4': 0}, 'female': {'Q1': 0, 'Q2': 0, 'Q3': 0, 
'Q4': 1}}
Male's and Female's: 2001: {'male': {'Q1': 0, 'Q2': 0, 'Q3': 0, 'Q4': 0}, 'female': {'Q1': 1, 'Q2': 0, 'Q3': 0, 
'Q4': 0}}
Male's and Female's: 2003: {'male': {'Q1': 0, 'Q2': 0, 'Q3': 0, 'Q4': 0}, 'female': {'Q1': 0, 'Q2': 0, 'Q3': 0, 
'Q4': 1}}

If not, let me know, I will modify it.

I encountered a few errors in the code that supposedly "worked fine", so I fixed them too and optimized things a bit in the process. Below is the result using a simple sample CSV file I created for testing purposes:

import csv
from pprint import pprint

#YOJ, QOJ, GEN = 17, 15, 3
YOJ, QOJ, GEN = 0, 1, 2  # For testing since no sample CSV provided.


results = {'males': {}, 'females': {}}

with open('1000 Records.csv') as csv_file:
    for row in csv.reader(csv_file):
        year_of_joining = int(row[YOJ])
        quarter_of_joining = int(row[QOJ])
        gender = 'males' if row[GEN] == 'M' else 'females'

        if year_of_joining not in results[gender]:
            results[gender][year_of_joining] = {f'Q{i + 1}': 0 for i in range(4)}

        QOJ_key = f'Q{quarter_of_joining+1}'  # Convert to dict key format.
        results[gender][year_of_joining][QOJ_key] += 1

years = sorted(results['males'].keys() | results['females'].keys())

data = {year: {'males': results['males'][year],
               'females': results['females'][year]}
        for year in years}

pprint(data, sort_dicts=False)

Sample output:

{1980: {'males': {'Q1': 0, 'Q2': 1, 'Q3': 1, 'Q4': 0},
        'females': {'Q1': 0, 'Q2': 0, 'Q3': 1, 'Q4': 0}},
 1981: {'males': {'Q1': 0, 'Q2': 0, 'Q3': 1, 'Q4': 0},
        'females': {'Q1': 0, 'Q2': 0, 'Q3': 0, 'Q4': 2}}}

Related