Skip to content Skip to sidebar Skip to footer

Count Males And Females Separately In A Nested Dict Format From Csv File

This code worked fine and it prints result in this format. I need results in a nested dict format like this. data = { 'year': { 'male': {'Q1': 1, '

Solution 1:

This is working solution:

import csv
import collections

data= {}

withopen('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 notin data:
            data[year_of_joining]={'male': {f'Q{i + 1}': 0for i inrange(4)}, 'female': {f'Q{i + 1}': 0for i inrange(4)}}
        data[year_of_joining][gender][quarter_of_joining] += 1

data = collections.OrderedDict(sorted(data.items())) # sortingfor 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.

Solution 2:

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)}

Solution 3:

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': {}}

withopen('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 notin results[gender]:
            results[gender][year_of_joining] = {f'Q{i + 1}': 0for i inrange(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}}}

Post a Comment for "Count Males And Females Separately In A Nested Dict Format From Csv File"