Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Inspect lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Selecting data · IR2 · 12 MIN

Review · Select with intent

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

Create a compact preview of the last two rows of df: show name, then age.

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

Your requirements

  1. Display a DataFrame.

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: select two columns in a chosen ordernamepricescoreA28B46C39scoreprice826493select two columns in a chosen order

A list of column names keeps a DataFrame, even when the list has just one name. The list also sets the column order.

A small example

df[["age", "name"]] shows age first and name second, with every original row.

What each choice does
Code or choiceMeaning
df[["age"]]Outer brackets select; inner brackets make a list. Result: one-column DataFrame.
df[["age", "name"]]Keep only these fields, in this order.
df.tail(2)[["name", "age"]]Combine a row preview with a column list.
Hint

Combine a row preview with a deliberate column order.

Reveal solution

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

df.tail(2)[["name", "age"]]
Your task · Task 1
  1. Create a compact preview of the last two rows of df: show name, then age.
  2. Display a DataFrame.

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.