File size: 1,839 Bytes
23f6dc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

def generate_labels(df, column_names, output_dir):
    """
    Generates a list of unique values for each column in the specified dataframe,
    and writes each list to a separate file with the specified filename.
    
    Args:
        df (pandas.DataFrame): The dataframe to generate code lists from.
        column_names (list): A list of column names to generate code lists for.
        output_dir (str): The directory to write the code list files to.
    """
    # Create the output directory if it doesn't exist
    os.makedirs(output_dir, exist_ok=True)
    
    # Iterate over the specified columns and generate a list of unique values for each column
    for column_name in column_names:
        if column_name == "ESCO_CODE":
            values = sorted(set(str(code) for code in df[column_name].tolist()))
        elif column_name == "ISCO_CODES":
            values = sorted(set(item for sublist in df[column_name].tolist() for item in sublist))
        elif column_name == "ESCO_LABELS":
            values = sorted(set(item for sublist in df[column_name].tolist() for item in sublist))
            values = sorted(set([str(val).strip() for val in values]))
        else:
            values = sorted(set(df[column_name].astype(str).tolist()))
        filename = os.path.join(output_dir, f"{column_name.lower()}.txt")
        with open(filename, "w") as f:
            f.write("\n".join(values))

columns_list = [
    "ISCO_CODE_1",
    "ISCO_CODE_2",
    "ISCO_CODE_3",
    "ISCO_CODE_4",
    "ISCO_LABEL_1",
    "ISCO_LABEL_2",
    "ISCO_LABEL_3",
    "ISCO_LABEL_4",
    "ISCO_CODES",
    "ESCO_CODE",
    "ESCO_LABELS",
    "ESCO_OCCUPATION",
]

for column_name in columns_list:
    generate_labels(
        isco_structure_df, 
        [column_name], 
        "../isco_esco_occupations_taxonomy/labels"
        )