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
A safe working copy · WR1 · 12 MIN

Review · Keep the evidence

Task 1 · Retrieve and combine

Exercises within this concept

  1. Task 1Retrieve and combineCurrent exercise
  2. Task 2Retrieve and combine
  3. Task 3Retrieve and combine
Task 1 · Pet adoption

Prepare a separate clean table for species="Cat", ordered by age largest first.

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

Your requirements

  1. Preserve df and display clean.

Your inputs

Pet adoption · 6 synthetic rows
namespeciesageweightroom
MiloCat34.2A
PepperDog718.5B
LunaCat23.6A
BeanRabbit42.4B
RexDog522A
NoriRabbit11.8B

Revisit the concept lesson →

Remember the idea

Concept sketch: edit clean; original df stays unchangeddfpriceA2B4cleanpriceA4B8df: 2, 4 unchangededit clean; original df stays unchanged

Make a separate working table before editing. Assigning another name to df alone still refers to the same object.

A small example

clean = df.copy() lets you select or sort clean while keeping the original df available.

What each choice does
Code or choiceMeaning
clean = dfAnother name for the same table; direct edits can affect df.
clean = df.copy()Independent working DataFrame for these scalar-valued tables.
clean = clean[...]Keep a selection in the working copy; display clean at the end.
Hint

Combine copying, filtering and sorting without assigning back to df.

Reveal solution

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

clean = df.copy()
clean = clean[clean["species"] == "Cat"].sort_values("age", ascending=False)
clean
Your task · Task 1
  1. Prepare a separate clean table for species="Cat", ordered by age largest first.
  2. Preserve df and display clean.

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.