Clean Counts From the File

Data with PythonMedium+10 XP+10

Clean Counts From the File

A wine lab exports its tasting results as a CSV file that uses semicolons between values, like the red wine file in the lesson. Every row has a quality score, except a few where the taster left the score blank. Some rows are exact repeats of earlier rows.

The lab wants the numbers behind a bar chart of the scores, worked out on clean data.

Task: write score_counts(path). path is the name of a CSV file like the one described. Its header row holds the column names, and one of them is quality. The starter code includes write_text(path, text), which saves text into a file and returns the path; the tests use it to create each file.

  1. Read the file.
  2. Remove rows that exactly repeat an earlier row (keep the first copy).
  3. Then remove rows with no quality score.
  4. Return a list of [score, count, percent], one per score that is left, sorted by score from lowest to highest. score and count are ints. percent is the share of the remaining rows with that score, as a percentage rounded to 1 decimal place.

At least one row has a score.

Examples

path = write_text("tasting_mon.csv", '"alcohol";"acidity";"quality"\n9.4;0.7;5\n9.8;0.88;5\n9.4;0.7;5\n11.2;0.5;6\n12.5;0.4;7\n10.1;0.6;\n')
returns [[5, 2, 50.0], [6, 1, 25.0], [7, 1, 25.0]]
path = write_text("tasting_tue.csv", '"alcohol";"quality"\n10.0;6\n10.0;6\n11.0;6\n9.5;4\n')
returns [[4, 1, 33.3], [6, 2, 66.7]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run