Q.Print the details of the car which gave the maximum mileage. (Dataset: the UCI 'auto-mpg' open dataset loaded into DataFrame autodf — 398 rows, nine attributes: mpg, cylinders, displacement, horsepower, weight, acceleration, model year, origin, car name.)
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Find the maximum mpg value in autodf, then filter the DataFrame for the row(s) matching that value. In the real UCI auto-mpg dataset, the single most fuel-efficient car is the Mazda GLC at 46.6 mpg (row index 322).
The general pattern: max-then-filter
Whenever you need "the record(s) that achieved an extreme value" rather than just the value itself, the pattern is always two steps:
- Find the extreme value with
.max()(or.min()). - Use that value as a boolean filter to pull out the matching row(s) — because a plain
.max()only gives you the number, not the rest of that row's data.
# Step 1: find the maximum mileage value
max_mpg_value = autodf['mpg'].max()
print("Maximum mpg in the dataset:", max_mpg_value)
# Step 2: filter the DataFrame for the row(s) with that mpg
car_with_max_mileage = autodf[autodf['mpg'] == max_mpg_value]
print(car_with_max_mileage)
Output:
Maximum mpg in the dataset: 46.6
mpg cylinders displacement horsepower weight acceleration model_year origin car_name
322 46.6 4 86.0 65.0 2110 17.9 80 3 mazda glc
Why these two lines do the job
autodf['mpg'].max()selects thempgcolumn (a Series of 398 values) and returns its single largest number. It does not tell you which car achieved it — a Series has no notion of "the rest of the row."autodf[autodf['mpg'] == max_mpg_value]is boolean indexing:autodf['mpg'] == max_mpg_valueproduces a Series ofTrue/False, one per row,Trueexactly where that row'smpgequals the maximum. Passing that boolean Series insideautodf[...]keeps only the matching row(s) — with every column intact, not justmpg. This is what actually answers "print the details of the car," since the question asks for the whole record, not just the number.
Reading the result
This particular car — a 1980-model, 4-cylinder Mazda GLC imported from Japan (origin = 3) — sits at the opposite extreme from the large 8-cylinder American cars that dominate head() (Exercise 3): far smaller displacement (86.0 vs 300+), far lower horsepower (65.0 vs 130–225), and less than two-thirds the weight (2110 lbs vs 3400–4400 lbs). This is exactly the trade-off the dataset is built to illustrate: mpg rises as engine size, horsepower and weight fall. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.