Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations

← Inspect lessons
Understanding values · Lesson 16 · 9 MIN

Find an extreme row by its label

Retrieve a whole observation using an extreme’s row label.

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

Understand the idea

idxmax() returns the largest known value’s row label, rather than its value or numbered position.

Illustrative data

Click the highlighted code to change the result.

df[].idxmax()
Source df
rowreadingtemperature
1011222
1061525
1031924
1091320
→
Result103

Largest reading: 19. Its row label is 103; its position is 2.

Follow the code

label = df["reading"].idxmax()
row = df.loc[label]
row[["hours", "reading"]]

What the syntax does

label = df["reading"].idxmax()
Find the label of the largest reading, not its value or position.
Labelled sensor readings
rowsamplehoursreading
101R1112
109R244
103R3619
150R438
106R527
118R6515
Your task · Follow

Which label identifies the largest reading?

  1. Use all rows of df.
  2. Display the index label of the largest reading.
  3. Use idxmax()
  4. Return the label rather than its reading or position.

Setup is included in the editor.

Hint

The result is a label suitable for loc, not the largest measurement.

Reveal solution

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

df["reading"].idxmax()

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.