diff --git a/recopytex/dashboard/pages/exams_scores/models.py b/recopytex/dashboard/pages/exams_scores/models.py index 4d7df5c..e3386af 100644 --- a/recopytex/dashboard/pages/exams_scores/models.py +++ b/recopytex/dashboard/pages/exams_scores/models.py @@ -2,7 +2,7 @@ # encoding: utf-8 from recopytex.database.filesystem.loader import CSVLoader -from recopytex.lib.dataframe import column_values_to_column +from recopytex.datalib.dataframe import column_values_to_column LOADER = CSVLoader("./test_config.yml") @@ -22,7 +22,7 @@ def get_record_scores(exam): def get_unstack_scores(exam): flat_scores = LOADER.get_exam_scores(exam) kept_columns = [col for col in LOADER.score_columns if col != "score"] - return column_values_to_column(flat_scores, "student_name", "score", kept_columns) + return column_values_to_column("student_name", "score", kept_columns, flat_scores) def get_students_from_exam(exam): diff --git a/recopytex/datalib/dataframe.py b/recopytex/datalib/dataframe.py new file mode 100644 index 0000000..d1b5845 --- /dev/null +++ b/recopytex/datalib/dataframe.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +def column_values_to_column(pivot_column, value_column, kept_columns, df): + """Pivot_column's values go to column with value_column under it, keeping kept_columns + + :param pivot_column: column name where value will become columns + :param value_column: column name where value will be under pivot_column + :param kept_columns: unchanged columns + :param df: DataFrame to work with + + :return: Stack dataframe + + """ + if pivot_column in kept_columns: + pivot_columns = kept_columns + else: + pivot_columns = kept_columns + [pivot_column] + + return df.set_index(pivot_columns).unstack(pivot_column)[value_column].reset_index()