Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations

← Inspect lessons
Reading the whole table · Lesson 25 · 9 MIN

Align Series when building a table

Combine labelled summaries without pairing unrelated values.

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

Understand the idea

A DataFrame built from Series matches their index labels, regardless of displayed order.

Illustrative data

pd.DataFrame({"mean": means, "records": counts})
means
LabelMean
A4
B9
counts
LabelRecords
B3
A2
→
Aligned result
Labelmeanrecords
A42
B93

Follow the code

pd.DataFrame(
    {
        "mean": means,
        "records": counts,
    }
)

What the syntax does

pd.DataFrame({"mean": means, "records": counts})
Match the Series by bench label.
series.index
Labels used to match values.
series.to_numpy()
Remove labels; table construction then matches positions.
Bench summary checks
benchreading
Ash9
Cedar6
Birch2
Ash10
Cedar8
Birch4
Birch60
Ash11
Your task · Follow

How can summaries in different orders be combined safely?

  1. Use supplied Series means and counts from df.
  2. Display a DataFrame with mean, then records, aligned by bench label.
  3. Keep bench labels as the index.

Setup is included in the editor.

Hint

Pass the Series themselves to the constructor so pandas can match labels.

Reveal solution

Keep the supplied setup and replace the Your work section with this example.

pd.DataFrame(
    {
        "mean": means,
        "records": counts,
    }
)

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.