JavaScript
x
24
24
1
import csv
2
3
4
result = {}
5
6
7
with open('1000 Records.csv', 'r') as csv_file:
8
9
csv_reader = csv.reader(csv_file)
10
11
for row in csv_reader:
12
13
year_of_joining = row[17]
14
15
half_of_joining = row[16]
16
17
if not year_of_joining in result:
18
19
result[year_of_joining] = {half_of_joining: half_of_joining}
20
21
else:
22
23
result[year_of_joining].update({half_of_joining: half_of_joining})
24
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
JavaScript
1
17
17
1
import csv
2
3
result = {}
4
5
with open('records.csv') as csv_file:
6
csv_reader = csv.DictReader(csv_file)
7
for row in csv_reader:
8
9
year_of_joining = int(row['Year of Join'])
10
half_of_joining = row['Half of Join']
11
12
13
if year_of_joining not in result:
14
result[year_of_joining] = {'H1': 0, 'H2': 0}
15
16
result[year_of_joining][half_of_joining] += 1
17