-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountData.py
More file actions
58 lines (48 loc) · 2.31 KB
/
countData.py
File metadata and controls
58 lines (48 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import csv
input_file_path = 'house_listing_clean.csv'
output_file_path = 'house_listing_count.csv'
count_new = {'True': 0, 'False': 0}
count_built = {}
count_bedrooms = {}
count_rooms = {}
count_energy_color = {}
count_energy_label = {}
count_ownership = {}
count_property_type = {}
count_is_sold = {'True': 0, 'False': 0}
with open(input_file_path, 'r', newline='') as f:
reader = csv.DictReader(f)
for row in reader:
if row['new'].strip() in count_new:
count_new[row['new'].strip()] += 1
built = row['built'].strip()
if built:
count_built[built] = count_built.get(built, 0) + 1
bedrooms = row['bedrooms'].strip()
if bedrooms:
count_bedrooms[bedrooms] = count_bedrooms.get(bedrooms, 0) + 1
rooms = row['rooms'].strip()
if rooms:
count_rooms[rooms] = count_rooms.get(rooms, 0) + 1
for field in ['energy_color', 'energy_label', 'ownership', 'propertyType', 'isSold']:
value = row[field].strip()
if value:
if field == 'energy_color':
count_energy_color[value] = count_energy_color.get(value, 0) + 1
elif field == 'energy_label':
count_energy_label[value] = count_energy_label.get(value, 0) + 1
elif field == 'ownership':
count_ownership[value] = count_ownership.get(value, 0) + 1
elif field == 'propertyType':
count_property_type[value] = count_property_type.get(value, 0) + 1
elif field == 'isSold':
count_is_sold[value] = count_is_sold.get(value, 0) + 1
with open(output_file_path, 'w', newline='') as out_file:
writer = csv.DictWriter(out_file, fieldnames=['Field', 'Value', 'Count'])
writer.writeheader()
for field, count_dict in [('new', count_new), ('built', count_built), ('bedrooms', count_bedrooms),
('rooms', count_rooms), ('EnergyColor', count_energy_color),
('EnergyLabel', count_energy_label), ('Ownership', count_ownership),
('PropertyType', count_property_type), ('isSold', count_is_sold)]:
for value, count in count_dict.items():
writer.writerow({'Field': field, 'Value': value, 'Count': count})