import csv result = {} with open('1000 Records.csv', 'r') as csv_file: csv_reader = csv.reader(csv_file) for row in csv_reader: year_of_joining = row[17] half_of_joining = row[16] if not year_of_joining in result: result[year_of_joining] = {half_of_joining: half_of_joining} else: result[year_of_joining].update({half_of_joining: half_of_joining})
the output I want that is like in the year 1980 people who join in 1st half and 2nd half in dictionary form
like this {1980:{H1:3, H2:0}, 1981:{H1:7, H2:8},…..}
Advertisement
Answer
You’re close, but you need to set 0
somewhere, and accumulate some results
import csv result = {} with open('records.csv') as csv_file: csv_reader = csv.DictReader(csv_file) for row in csv_reader: year_of_joining = int(row['Year of Join']) half_of_joining = row['Half of Join'] if year_of_joining not in result: result[year_of_joining] = {'H1': 0, 'H2': 0} result[year_of_joining][half_of_joining] += 1