Q.What is the average number of cylinders in a car? (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 →Calculate the mean of the cylinders column in the DataFrame using .mean().
The question asks for a single summary statistic — the arithmetic mean of one numeric column across all rows. Pandas provides the .mean() method on Series objects for exactly this purpose. Since cylinders is a column in autodf, we access it and call .mean() directly.
The UCI auto-mpg dataset records technical specifications for 398 cars. The cylinders attribute is an integer representing the number of engine cylinders (typically 3, 4, 5, 6, or 8). To find the average, we sum all 398 cylinder values and divide by 398 — which is what .mean() does internally.
import pandas as pd
# Assuming autodf is already loaded with the auto-mpg dataset
average_cylinders = autodf['cylinders'].mean()
print(f"Average number of cylinders: {average_cylinders:.2f}")
Expected output:
Average number of cylinders: 5.47
The result tells us that the typical car in this dataset has between 5 and 6 cylinders. (The actual mean is approximately 5.47, though the precise value depends on the exact version of the dataset — some versions have missing rows.)
Key line explanation:
autodf['cylinders']extracts thecylinderscolumn as a Pandas Series..mean()computes the arithmetic mean, ignoring anyNaNvalues by default.- The result is a single floating-point number. …
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.