Add function to create ISCO-08 hierarchy from CSV file
Browse files
isco.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# filename: isco.py
|
2 |
+
import csv
|
3 |
+
|
4 |
+
|
5 |
+
def create_hierarchy(filename):
|
6 |
+
"""
|
7 |
+
Creates a dictionary where keys are nodes and values are sets of parent nodes representing the hierarchy of the ISCO-08 codes from the "unit" column of the isco_structure.csv file.
|
8 |
+
|
9 |
+
Args:
|
10 |
+
- filename: A string representing the path to the CSV file containing the ISCO-08 codes and their hierarchy.
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
- A dictionary where keys are ISCO-08 unit codes and values are sets of their parent codes.
|
14 |
+
"""
|
15 |
+
isco_hierarchy = {}
|
16 |
+
|
17 |
+
with open(filename, newline="") as csvfile:
|
18 |
+
reader = csv.DictReader(csvfile)
|
19 |
+
for row in reader:
|
20 |
+
# Extract unit group level code (4 digits)
|
21 |
+
unit_code = row["unit"].zfill(4)
|
22 |
+
# Extract the parent code for the unit group level, which is the minor group level (3 digits)
|
23 |
+
parent_code = unit_code[:3]
|
24 |
+
|
25 |
+
# Add the unit code to the hierarchy with its parent code
|
26 |
+
isco_hierarchy[unit_code] = {parent_code}
|
27 |
+
|
28 |
+
# Additionally, we can add the parent's parent codes if needed
|
29 |
+
# For example, the major group level (1 digit) and sub-major group level (2 digits)
|
30 |
+
major_code = unit_code[0]
|
31 |
+
sub_major_code = unit_code[:2]
|
32 |
+
isco_hierarchy[unit_code].update({major_code, sub_major_code})
|
33 |
+
|
34 |
+
return isco_hierarchy
|
35 |
+
|
36 |
+
|
37 |
+
# Example usage:
|
38 |
+
# hierarchy = create_hierarchy("ISCO_structure.csv")
|
39 |
+
# print(hierarchy)
|