Squeeze It Into 0 to 1

Data with PythonMedium+10 XP+10

Squeeze It Into 0 to 1

A music app wants to compare songs by tempo and length. Those columns use very different numbers, so it rescales each column to run from 0 to 1:

  • the smallest value becomes 0
  • the largest value becomes 1
  • everything else sits in between, at (value − smallest) ÷ (largest − smallest)

Task: write zero_to_one(table, column).

  • table is a dictionary of lists, one list per column. Turn it into a DataFrame.
  • Rescale the column named column and return the new values as a list, in the original row order.
  • Round every value to 3 decimal places.
  • If every value in the column is the same, the formula would divide by zero. Return 0.0 for every row instead.

Examples

table = {"song": ["Aria", "Bloom", "Coda", "Drift"], "bpm": [90, 120, 150, 110]}
column = "bpm"
returns [0.0, 0.5, 1.0, 0.333]
table = {"song": ["Aria", "Bloom", "Coda"], "seconds": [200, 185, 245]}
column = "seconds"
returns [0.25, 0.0, 1.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