Feat: write column_values_to_column

This commit is contained in:
Bertrand Benjamin 2021-04-18 17:59:46 +02:00
parent a8b2ac455d
commit 00d81d694a
2 changed files with 23 additions and 2 deletions

View File

@ -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):

View File

@ -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()