Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Inspect lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Reading the whole table · I18S · 8 MIN

Answer one numerical question

Choose a direct summary for a specific question.

Exercises within this concept

  1. FollowFollow an average calculationCurrent exercise
  2. ChangeChoose a total
  3. TransferChoose a robust middle

Understand the idea

Choose the reduction that answers the question. A total, an average and a middle value describe different things.

Concept sketch: one column → specific numerical answersprice246methodresultmean4sum12q755one column → specific numerical answers
Illustration · not the exercise output

A small example

For [2, 4, 12]: sum is 18, mean is 6 and median is 4. For [2, 4, 8, 10], median is (4 + 8) / 2 = 6.

Follow the code

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

df["price"].mean()

What each part does

df["price"]
Choose the numeric column whose values answer the question.
.mean()
Average its known values.
Other choices for later exercises
.median()
Find the middle known value instead.
.sum() / .count()
Add known values / count known values.
.min() / .max()
Smallest / largest known value.
.quantile(0.75)
The 75th percentile of known values.

Your inputs

The editable setup on the right creates df. 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

Your task · Follow

  1. Using df price, calculate the mean price.
  2. Leave the numeric result on the final line.
Hint

mean answers an average question, whereas sum answers a total question.

Reveal solution

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

df["price"].mean()
Your task · Follow
  1. Using df price, calculate the mean price.
  2. Leave the numeric result on the final line.

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.