Daniel Duckworth
dump
23f6dc8
raw
history blame
1.84 kB
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"
)