Create a DataFrame from a dictionary and export it to a CSV file

Task: MIL-004#1
Task: MIL-004#2
This commit is contained in:
2026-10-13 15:32:19 +08:00
parent 99558b4ef8
commit 844abb7482
3 changed files with 78 additions and 2 deletions
+24 -2
View File
@@ -3,8 +3,9 @@
#
# Every value that the day-25 lectures fix lives here, so that no other module
# contains a magic string or number: the name of the data file, the names of
# its columns, the factors of the Fahrenheit conversion and the name of the
# exported file.
# its columns, the factors of the Fahrenheit conversion, the name of the
# exported file, the table that is created from a dictionary and the message
# for a missing optional dependency.
"""Constants of the weather data project, as fixed by the day-25 lectures."""
@@ -46,3 +47,24 @@ FAHRENHEIT_DENOMINATOR: Final[int] = 5
## @brief Offset of the Celsius to Fahrenheit conversion: `temp * 9 / 5 + 32`.
FAHRENHEIT_OFFSET: Final[int] = 32
## @brief Name of the column that holds the student names of the lecture's table.
COLUMN_STUDENTS: Final[str] = "students"
## @brief Name of the column that holds the scores of the lecture's table.
COLUMN_SCORES: Final[str] = "scores"
## @brief Student names of the table that the lecture creates from a dictionary.
STUDENTS: Final[tuple[str, ...]] = ("Amy", "James", "Angela")
## @brief Scores of the students, in the order of `STUDENTS`.
SCORES: Final[tuple[int, ...]] = (76, 56, 65)
## @brief Name of the day that the main flow selects and converts.
MONDAY: Final[str] = "Monday"
## @brief Message printed when `pandas`, an optional extra, is not installed.
PANDAS_MISSING_MESSAGE: Final[str] = (
"pandas is not installed; the pandas part needs the extra: "
'pip install "weather[pandas]"'
)
+39
View File
@@ -0,0 +1,39 @@
## @file export.py
# @brief Creates a `DataFrame` from a dictionary and exports it to a CSV file.
#
# This is the end of the second lecture: the table of students and scores is
# built from a dictionary and written with `DataFrame.to_csv`. Nothing is
# written unless `export_frame` is called with a path or its default name.
"""Creates a DataFrame from a dictionary and exports it to a CSV file."""
from pathlib import Path
import pandas
from weather.constants import (
COLUMN_SCORES,
COLUMN_STUDENTS,
EXPORT_FILE_NAME,
SCORES,
STUDENTS,
)
def create_scores_frame() -> pandas.DataFrame:
"""! @brief Create the table of students and scores from a dictionary.
@return A table with the columns `students` and `scores`, one row per student.
"""
data_dict = {COLUMN_STUDENTS: list(STUDENTS), COLUMN_SCORES: list(SCORES)}
return pandas.DataFrame(data_dict)
def export_frame(frame: pandas.DataFrame, path: Path | str = EXPORT_FILE_NAME) -> None:
"""! @brief Write a table to a CSV file, the row index included.
@param frame The table to write.
@param path Path of the file to write; `new_data.csv` in the working folder
by default.
"""
frame.to_csv(path)