Melt a wide leaderboard into long rows and drop missing scores

Easypandas

Why it matters Long format is what plotting libraries, groupby and SQL loaders expect, and eval results usually arrive wide.

Leaderboards are stored wide, one column per benchmark, but plotting and joining need long rows. Models that skipped a benchmark have NaN there, and those cells should not become rows.

Write to_long(df) where df has a column model (str) and one float column per benchmark; cells may be NaN.

Return a DataFrame with columns exactly [model, benchmark, score], one row per non-missing cell, sorted by model then benchmark ascending, with a plain 0..n-1 index.

Signaturedef to_long(df)

Example

model ['a', 'b'], math [0.5, NaN], code [0.7, 0.9] gives model ['a', 'a', 'b'], benchmark ['code', 'math', 'code'], score [0.7, 0.5, 0.9].

Understand the idea

Wide data has one column per variable, like a spreadsheet with a column per benchmark. Long data has one row per observation: (model, benchmark, score). melt goes from wide to long; pivot goes back.

Worked example: row a has math 0.5 and code 0.7; row b has math NaN and code 0.9. melt gives four rows: (a, math, 0.5), (b, math, NaN), (a, code, 0.7), (b, code, 0.9). Dropping NaN leaves three, and sorting by model then benchmark gives (a, code, 0.7), (a, math, 0.5), (b, code, 0.9).

Rule: long = wide.melt(id_vars=[keys], var_name='variable', value_name='value'). The number of rows is keys x value columns before dropping missing cells.

Eval dashboards and experiment trackers store results long so new benchmarks need no new columns.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: exampleto_long(_d1()).to_dict('list')
  • Sample 2: several gapsto_long(_d2()).to_dict('list')
  • Test 3: all missingto_long(_d3()).to_dict('list')
  • Test 4: columnslist(to_long(_d2()).columns)