Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Wrangle / Preprocess lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Groups & reshaping · W23 · 8 MIN

Long → wide

Reshape unique identifier/measurement pairs.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt a requirement
  3. TransferChoose and combine

Understand the idea

pivot spreads a measurement-label column across new columns. It rearranges existing values without calculating a summary.

Concept sketch: long → wide: unique id/variable pairsidvarvalAx2Ay4Bx3idxyA24B3NaNlong → wide: unique id/variable pairs
Illustration · not the exercise output

A small example

Ada · age · 4 and Ada · weight · 8 become one Ada row with age=4 and weight=8.

Follow the code

Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.

long.pivot(index="candy", columns="measure", values="value")

What each part does

long.pivot(...)
Reshape the supplied long table into wider rows.
index="candy"
Use each candy name to identify an output row.
columns="measure"
Turn each measure label into an output column.
values="value"
Put each corresponding value into its new cell.

Your inputs

The editable setup on the right creates df and the additional inputs shown below. Run executes the setup and your work from top to bottom.

Candy shop · 6 synthetic rows
candyflavourpriceratingshelf
Gummy Bearfruity1.24.1A
Choco Popchocolate2.14.6B
Mint Bitemint1.53.8A
Berry Loopfruity2.84.4B
Cocoa Cubechocolate3.44.9A
Lemon Dropfruity1.84B
long · prepared measurement table
candymeasurevalue
Gummy Bearprice1.2
Choco Popprice2.1
Mint Biteprice1.5
Berry Loopprice2.8
Cocoa Cubeprice3.4
Lemon Dropprice1.8
Gummy Bearrating4.1
Choco Poprating4.6
Mint Biterating3.8
Berry Looprating4.4
Cocoa Cuberating4.9
Lemon Droprating4

Your task · Follow

  1. The supplied long table contains candy, measure and value.
  2. Pivot it back with candy on rows, measure on columns and value in cells.
  3. Use: pivot().
Hint

pivot needs one value per identifier/measure pair and does not aggregate repeats.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

long.pivot(index="candy", columns="measure", values="value")
Optional stretch

Duplicate one row in long with pd.concat([long, long.head(1)]). Why does pivot reject it?

Your task · Follow
  1. The supplied long table contains candy, measure and value.
  2. Pivot it back with candy on rows, measure on columns and value in cells.
  3. Use: pivot().

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Each run executes all editor code in a fresh Python session. Display a value by leaving it on the final line.

Python starts when you open a lesson.

Output

Run your code to see what Python returns.