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 responsible finish · WR5 · 12 MIN

Review · Prepare responsibly

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
  4. Task 4Retrieve and combine
Task 1 · Pet adoption

Combine supplied first and second, then keep records with age above 2.

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.

Your requirements

  1. Display them sorted by age, largest first, with a fresh consecutive index.

Your inputs

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

Revisit the concept lesson →

Remember the idea

Concept sketch: concat: append rows from both tablesfirst24+second6indexvalue021426concat: append rows from both tables

concat appends tables in the order listed. It matches column names, rather than looking for matching records.

A small example

Batches with indices [0, 1] and [0, 1] produce [0, 1, 0, 1], or [0, 1, 2, 3] with ignore_index=True.

What each choice does
Code or choiceMeaning
pd.concat([first, second])Append second below first and retain existing row labels.
ignore_index=TrueNumber the combined rows consecutively from zero.
Hint

Finish filtering and sorting before assigning presentation indices.

Reveal solution

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

combined = pd.concat([first, second], ignore_index=True)
combined[combined["age"] > 2].sort_values("age", ascending=False).reset_index(drop=True)
Your task · Task 1
  1. Combine supplied first and second, then keep records with age above 2.
  2. Display them sorted by age, largest first, with a fresh consecutive index.

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.