Fix question IDs.

master
junos 2023-07-03 20:11:11 +02:00
parent 2f22f2052a
commit c0236b251c
1 changed files with 15 additions and 1 deletions

View File

@ -136,7 +136,7 @@ def reassign_question_ids(df_cope_cleaned: pd.DataFrame) -> pd.DataFrame:
) )
# Tabulate all possible answers to each question (group by question ID). # Tabulate all possible answers to each question (group by question ID).
df_cope_fixed = df_cope_cleaned.copy() # First, check that we anticipated all esm instructions.
for q_id in dict_COPE_question_ids.keys(): for q_id in dict_COPE_question_ids.keys():
# Look for all questions ("instructions") occurring in the dataframe. # Look for all questions ("instructions") occurring in the dataframe.
actual_questions = df_esm_cope_unique_questions.loc[ actual_questions = df_esm_cope_unique_questions.loc[
@ -154,6 +154,20 @@ def reassign_question_ids(df_cope_cleaned: pd.DataFrame) -> pd.DataFrame:
raise KeyError(actual_questions[~questions_matches]) raise KeyError(actual_questions[~questions_matches])
# In case there is an unexpected answer, raise an exception. # In case there is an unexpected answer, raise an exception.
# Next, replace question IDs.
df_cope_fixed = df_cope_cleaned.copy()
df_cope_fixed["question_id"] = df_cope_cleaned["esm_instructions"].apply(
lambda x: next(
(
key
for key, values in dict_COPE_question_ids.items()
if x.startswith(values)
),
None,
)
)
# Finally, increment numeric answers.
try: try:
df_cope_fixed = df_cope_fixed.assign( df_cope_fixed = df_cope_fixed.assign(
esm_user_score=lambda x: x.esm_user_answer_numeric + 1 esm_user_score=lambda x: x.esm_user_answer_numeric + 1