Sub topic 9
Looking at One Variable at a Time
This chapter assumes you can use pandas, you have run the setup, loading, and cleaning steps from earlier chapters, and you know mean, median, and distribution. It covers univariate analysis, which means studying one column on its own before you compare columns.
Why start with single variables
Each column has its own shape, range, and problems. If you know these first, later comparisons are easier to read. A skewed column, for example, can make a correlation look weaker or stronger than it is.
The method depends on the column type:
| Column type | Summary | Plot |
|---|---|---|
| Numeric | count, mean, median, spread, extremes | histogram, box plot |
| Categorical | counts and shares per level | bar chart |
| Datetime | range, orders per period | line of counts over time |
Numeric columns
Start with numbers, then plot. The gap between mean and median is a quick check for skew. Percentiles show how far the tail reaches.
amount = df["amount"]
summary = amount.describe(percentiles=[0.01, 0.25, 0.5, 0.75, 0.99])
summary["skew"] = amount.skew()
print(summary.round(2))
print("mean - median:", round(amount.mean() - amount.median(), 2))A positive skew and a mean well above the median suggest a long right tail. A histogram confirms this. Try several bin counts, because one choice can hide structure.
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
amount.plot.hist(bins=30, ax=axes[0])
axes[0].set_title("Order amount, 30 bins")
axes[0].set_xlabel("Amount")
axes[0].set_ylabel("Number of orders")
np.log1p(amount).plot.hist(bins=30, ax=axes[1])
axes[1].set_title("Order amount, log scale")
axes[1].set_xlabel("log(1 + amount)")
axes[1].set_ylabel("Number of orders")
plt.tight_layout()
plt.show()The log transform here is only for viewing. Do not overwrite the amount column with it unless you decide that later and record why.
Categorical columns
For categories, count the levels and show shares. Keep missing values visible with dropna=False.
region_counts = df["region"].value_counts(dropna=False)
region_share = df["region"].value_counts(normalize=True, dropna=False).round(3)
print(pd.DataFrame({"count": region_counts, "share": region_share}))
region_counts.plot.barh()
plt.title("Orders per region")
plt.xlabel("Number of orders")
plt.ylabel("Region")
plt.show()Look for levels with very few rows, near-duplicate labels such as north and north , and one level that dominates. The cleaning step should already have removed whitespace and case differences, so any survivors are worth a note.
Dates
For a datetime column, check the range, then count rows per period to find gaps or spikes.
print(df["order_date"].min(), "to", df["order_date"].max())
df.set_index("order_date").resample("W").size().plot()
plt.title("Orders per week")
plt.xlabel("Week")
plt.ylabel("Number of orders")
plt.show()A flat zero stretch may be an outage in data collection, not a real pause in sales.
Common errors
- Trusting one histogram. Change the bin count before you describe the shape.
- Reporting the mean of a skewed column without the median.
- Dropping outliers because they look odd. First ask whether they are errors or real orders.
- Plotting with missing values hidden, so a category's share looks larger than it is.
Exercise
A column has a mean of 120 and a median of 45. What does this suggest about its shape, and what two checks would you run next?
Show answer
It suggests a right-skewed distribution with a few large values pulling the mean up. Plot a histogram (try several bin counts, or a log view) and inspect the upper percentiles, such as the 99th, to see whether the large values are real or errors.