Fees by Join Year

Data with PythonMedium+10 XP+10

Fees by Join Year

A swimming club keeps a member list in a CSV file with three columns: name, joined (a date written YYYY-MM-DD) and fee. The file was put together by hand, so:

  • some rows were pasted in twice, exactly the same
  • fees are typed in different ways: 25, £25, £30 with stray spaces, and sometimes a word like free or waived

Task: write fees_for_year(path, year). The starter code includes write_text(path, text), which saves CSV text to a file and returns its path. Each test passes that path straight to fees_for_year.

  1. Load the file at path with pandas.
  2. Remove rows that are exact copies of an earlier row, keeping the first.
  3. Clean fee: remove spaces from both ends and remove the £ sign, then convert it to numbers. Anything that still can't be read becomes missing.
  4. Turn joined into real dates.
  5. Keep only members who joined in year.

Return a list of two things: how many members joined in year (a whole number), and the total of their fees that could be read, rounded to 2 decimal places. A fee that couldn't be read adds nothing. If nobody joined in year, return [0, 0.0].

Every file has at least one fee written with a £ sign.

Examples

path = write_text("club_a.csv", "name,joined,fee\nAna,2023-04-02,£25\nBen,2024-01-15,30\nCara,2024-06-30, £30 \nAna,2023-04-02,£25\nDev,2024-09-01,free\n")
year = 2024
returns [3, 60.0]
path = write_text("club_b.csv", "name,joined,fee\nAna,2023-04-02,£25\nBen,2024-01-15,30\nCara,2024-06-30, £30 \nAna,2023-04-02,£25\nDev,2024-09-01,free\n")
year = 2023
returns [1, 25.0]

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