Q.Solved Case Study based on Open Datasets: UCI dataset is a collection of open datasets, available to the public for experimentation and research purposes. 'auto-mpg' is one such open dataset. It contains data related to fuel consumption by automobiles in a city, measured in miles per gallon (mpg). The data has 398 rows (items/instances) and nine columns (attributes): mpg, cylinders, displacement, horsepower, weight, acceleration, model year, origin, car name. Three attributes — cylinders, model year and origin — have categorical values; car name is a string with a unique value for every row; the remaining five attributes have numeric values. The data is downloaded from the UCI data repository at http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/. Load auto-mpg.data into a DataFrame autodf.
This is a Data Preprocessing task: loading the raw UCI auto-mpg.data file into a pandas DataFrame with proper column names and missing-value handling, using pd.read_csv() with a whitespace separator — not a fixed-width reader.
Understanding the file format
The UCI auto-mpg dataset is a classic regression dataset for predicting fuel efficiency. The raw auto-mpg.data file has no header row, and its fields are separated by runs of whitespace (the columns only look fixed-width because the numbers are right-padded for readability — the actual separator is one-or-more spaces, not a fixed character position). Each car's name is the last field and is wrapped in double quotes (e.g. "chevrolet chevelle malibu") specifically because it can itself contain spaces — without the quotes, a plain whitespace split would shred a multi-word car name into several extra columns.
Missing values in the file are marked with a literal ? (this happens in the horsepower column).
The question asks us to load this into a DataFrame called autodf. This is a pure data-loading task, not an analysis one — the challenge is telling pandas how to split the whitespace-separated fields, supply the missing header, and treat ? as missing.
The right tool: pd.read_csv() with a whitespace separator
import pandas as pd
# Column names as given in the problem
columns = ['mpg', 'cylinders', 'displacement', 'horsepower', 'weight',
'acceleration', 'model_year', 'origin', 'car_name']
# Load the whitespace-separated file
autodf = pd.read_csv(
'auto-mpg.data',
sep=r'\s+',
header=None,
names=columns,
na_values='?'
)
na_values='?' is critical here. The dataset uses ? to mark missing horsepower readings. Without it, pandas would read those cells as the literal string '?', making the whole horsepower column an object (text) column instead of a numeric one — and you would not be able to run .mean(), .median() or any other numeric operation on it.
What the code does, step by step
sep=r'\s+'— The regex\s+matches one or more whitespace characters (spaces or tabs) between fields, so it correctly splits each line regardless of how many spaces separate two values. Because pandas' parser respects the double quotes aroundcar_name, the spaces inside a quoted car name are not treated as separators — the whole quoted phrase stays one field.header=None— The file has no column-name row, so pandas must not treat the first data row as a header.names=columns— Supplies the nine attribute names from the problem statement, in file order: mpg, cylinders, displacement, horsepower, weight, acceleration, model year, origin, car name.na_values='?'— Converts every?entry toNaNso the column keeps a numeric dtype.
Verifying the load
print(autodf.shape) # Should be (398, 9)
print(autodf.dtypes) # horsepower should be float64, not object
print(autodf.head()) # Preview first 5 rows
Output (first 5 rows):
mpg cylinders displacement horsepower weight acceleration model_year origin car_name
0 18.0 8 307.0 130.0 3504.0 12.0 70 1 chevrolet chevelle malibu
1 15.0 8 350.0 165.0 3693.0 11.5 70 1 buick skylark 320
2 18.0 8 318.0 150.0 3436.0 11.0 70 1 plymouth satellite
3 16.0 8 304.0 150.0 3433.0 12.0 70 1 amc rebel sst
4 17.0 8 302.0 140.0 3449.0 10.5 70 1 ford torino
A tempting alternative is pd.read_fwf() (fixed-width reader), since the printed columns line up visually. But read_fwf() infers column boundaries from character position, and it has no way to know that a car name's internal spaces belong to a single field rather than marking a new column boundary — it would either mis-split multi-word names or need explicit colspecs supplied by hand for every column. read_csv(sep=r'\s+', ...) is the standard, robust choice for this file precisely because it splits on whitespace runs while still respecting the double quotes around car_name.
Handling missing values (optional but good practice)
# Check for missing values
print(autodf.isnull().sum())
# horsepower has 6 missing values in this dataset
autodf['horsepower'] = autodf['horsepower'].fillna(autodf['horsepower'].median())
The horsepower column has exactly 6 missing rows in the real auto-mpg dataset (out of 398). Filling with the median is a reasonable default since horsepower is roughly symmetric and the median is robust to the dataset's few very high-horsepower outliers.
The auto-mpg.data file is loaded into autodf using pd.read_csv('auto-mpg.data', sep=r'\s+', header=None, names=columns, na_values='?') — a whitespace-delimited read, not a fixed-width one — supplying the nine column names manually (since the file has no header) and converting the file's ? markers to NaN so horsepower stays numeric. The resulting DataFrame has 398 rows and 9 columns.
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.