danieldux's picture
Add function to create ISCO-08 hierarchy from CSV file
7375cc5
raw
history blame
1.46 kB
# filename: isco.py
import csv
def create_hierarchy(filename):
"""
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.
Args:
- filename: A string representing the path to the CSV file containing the ISCO-08 codes and their hierarchy.
Returns:
- A dictionary where keys are ISCO-08 unit codes and values are sets of their parent codes.
"""
isco_hierarchy = {}
with open(filename, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# Extract unit group level code (4 digits)
unit_code = row["unit"].zfill(4)
# Extract the parent code for the unit group level, which is the minor group level (3 digits)
parent_code = unit_code[:3]
# Add the unit code to the hierarchy with its parent code
isco_hierarchy[unit_code] = {parent_code}
# Additionally, we can add the parent's parent codes if needed
# For example, the major group level (1 digit) and sub-major group level (2 digits)
major_code = unit_code[0]
sub_major_code = unit_code[:2]
isco_hierarchy[unit_code].update({major_code, sub_major_code})
return isco_hierarchy
# Example usage:
# hierarchy = create_hierarchy("ISCO_structure.csv")
# print(hierarchy)