Data Processing · Topic 1 of 3
Exploring Data: Summary Statistics, Outliers and Charts
Databricks ML Associate: summary statistics with summary() and dbutils, outlier removal by standard deviation or IQR, charts and comparing two features.
- 5 min
- summary() adds the 25%, 50% and 75% percentiles to what describe() returns, and accepts the names of the statistics you want.
- dbutils.data.summarize(df) renders a column profile in the notebook; precise=True trades speed for exactness.
- IQR bounds are Q1 - 1.5 x IQR and Q3 + 1.5 x IQR; the standard deviation rule keeps mean +/- k x std dev.
What it is
Exploratory data analysis is the first look at a DataFrame before any feature work. You measure each column's center, spread and missing values, find and remove extreme values, and draw charts that show the distribution.
On Databricks the main tools are the Spark DataFrame method summary(), the notebook utility dbutils.data.summarize(), simple filters for outliers and the notebook's built-in charts.
How it works
Summary statistics
| Call | What you get |
|---|---|
| df.describe() | count, mean, stddev, min, max |
| df.summary() | count, mean, stddev, min, 25%, 50%, 75%, max |
| df.summary("count", "min", "75%", "max") | Only the statistics you name |
| dbutils.data.summarize(df) | A rendered profile with statistics and histograms for each column |
The percentiles from summary() are approximate, which keeps the call fast on large data. Both summary() and describe() return a new DataFrame, so you can show it or keep working with it.
dbutils.data.summarize() displays the result in the notebook instead of returning a DataFrame. By default (precise=False) some figures are approximations to cut run time; precise=True makes all statistics exact except the histograms and percentiles of numeric columns. It reads the complete DataFrame, so it can be slow on very large inputs.
df.summary().show()
df.select("price", "sqft").summary("mean", "50%").show()
dbutils.data.summarize(df, precise=True)
Removing outliers
Two rules are common. The standard deviation rule keeps rows within k standard deviations of the mean, often k = 3. The IQR rule keeps rows between Q1 - 1.5 x IQR and Q3 + 1.5 x IQR, where IQR = Q3 - Q1.
| Rule | Worked numbers | Kept range |
|---|---|---|
| 3 standard deviations | mean 50, std dev 10 | 20 to 80 |
| 1.5 x IQR | Q1 20, Q3 40, IQR 20 | -10 to 70 |
The IQR rule uses quartiles, so the extreme values it is hunting barely move its bounds. The standard deviation rule assumes a roughly normal shape, and a few huge values inflate the mean and the standard deviation, which widens the bounds and lets outliers survive.
from pyspark.sql import functions as F
q1, q3 = df.approxQuantile("price", [0.25, 0.75], 0.01)
iqr = q3 - q1
clean = df.filter(F.col("price").between(q1 - 1.5 * iqr, q3 + 1.5 * iqr))
approxQuantile(column, probabilities, relativeError) returns a list of values; a relativeError of 0 computes exact quantiles at a higher cost. Note that the filter also drops rows where price is null, because a comparison with null is never true.
s = df.select(F.mean("price").alias("m"), F.stddev("price").alias("sd")).first()
clean = df.filter(F.abs(F.col("price") - s["m"]) <= 3 * s["sd"])
Charts for one feature
Calling display(df) in a Databricks notebook shows a table with options to add a visualization or a data profile. The right chart depends on the type of the feature.
| Feature type | Chart | What it shows |
|---|---|---|
| Categorical | Bar chart of counts | Frequency of each level, rare levels |
| Continuous | Histogram | Shape, skew, multiple peaks |
| Continuous | Box plot | Median, quartiles, outlier points |
Comparing two features
| Pair | Numeric method | Chart |
|---|---|---|
| Two continuous | Pearson correlation (linear) or Spearman correlation (rank, monotonic) | Scatter plot |
| Two categorical | Contingency table (crosstab) and chi-square test of independence | Stacked or grouped bar chart |
df.stat.corr("a", "b") returns the Pearson correlation of two numeric columns. For a correlation matrix, or for Spearman, use pyspark.ml.stat.Correlation on a vector column built with VectorAssembler.
from pyspark.ml.feature import VectorAssembler
from pyspark.ml.stat import Correlation
vec = VectorAssembler(inputCols=["sqft", "price"], outputCol="v").transform(df.dropna())
matrix = Correlation.corr(vec, "v", "spearman").head()[0]
For two categorical columns, df.stat.crosstab("plan", "region") counts each pair of levels. ChiSquareTest in pyspark.ml.stat tests whether categorical features are independent of a categorical label; a small p-value suggests they are related.
Correlation only measures association between numbers. A correlation coefficient on two category codes is meaningless, because the codes have no real order or distance.
How the exam asks about this
- "Get the mean, standard deviation and quartiles of every numeric column in a Spark DataFrame." Use df.summary(); describe() has no quartiles.
- "A column has a few extreme values and a skewed distribution; remove outliers robustly." Use the IQR rule with approxQuantile.
- "Check whether subscription plan and region are related." Both are categorical, so use a crosstab and a chi-square test, not a correlation.
What to remember for the exam
- summary() adds the 25%, 50% and 75% percentiles to what describe() returns, and accepts the names of the statistics you want.
- dbutils.data.summarize(df) renders a column profile in the notebook; precise=True trades speed for exactness.
- IQR bounds are Q1 - 1.5 x IQR and Q3 + 1.5 x IQR; the standard deviation rule keeps mean +/- k x std dev.
- Histograms and box plots suit continuous features; bar charts of counts suit categorical features.
- Two continuous features: correlation and a scatter plot. Two categorical features: crosstab and chi-square.
- Pearson measures linear association; Spearman uses ranks and catches monotonic relationships.
The standard deviation rule is distorted by the very outliers it should remove. On skewed data, prefer the IQR rule or look at a box plot first.
Common mistakes
Picking describe() when quartiles are needed. describe() stops at count, mean, stddev, min and max. Only summary() returns percentiles.
Correlating two categorical columns. Indexing "red", "green", "blue" as 0, 1, 2 and computing Pearson gives a number with no meaning. Use a contingency table and a chi-square test.
Removing outliers without looking at them. An extreme value can be a data entry error or a real, important case such as fraud. Check the business meaning before filtering, and fit the bounds on training data only.
Check yourself
Which call returns quartiles for every numeric column of a Spark DataFrame?
df.summary(), which returns count, mean, stddev, min, 25%, 50%, 75% and max by default; describe() has no percentiles.
With Q1 = 30 and Q3 = 50, which values does the IQR rule remove?
IQR is 20, so values below 30 - 30 = 0 or above 50 + 30 = 80 are removed.
How do you compare two continuous features?
Use a scatter plot and a correlation coefficient: Pearson for a linear relationship, Spearman for a monotonic one or when outliers are present.
Topic 1 of 3 in Data Processing
Test yourself on Data Processing
The full Databricks Machine Learning Associate question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by Databricks. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.