Interview's MateData Science
Interview's Mate

Data Science

Python → ML → MLOps

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Science topics
Lesson 18/ 160
01. Python & Data Wrangling

Univariate & Bivariate Analysis

Summarize single variables and relationships between pairs before modeling.

IntermediateDistributions · Cross-tabs3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

First look at each column by itself, then look at how two columns behave together.

Think of it like…

Getting to know a team: first meet each person individually (univariate: what are they like?), then see how pairs interact (bivariate: who works well with whom?). Only then do you plan the project.

Where you’ve seen it

In a food-delivery dataset, delivery_minutes has mean 38 and median 31 with a long right tail (a few 3-hour orders). The histogram shows two bumps (lunch and dinner), the box plot by city shows Bengaluru's median is 9 minutes longer than Pune's, and a cross-tab shows the share of late orders jumps from 8 percent to 27 percent when it rains. None of those are visible in one average.

Words you’ll hear

Central tendency / spread
Mean, median, mode / standard deviation, IQR, range.
Skewness
Asymmetry of a distribution; a long right tail pulls the mean above the median.
Cross-tab
A table of counts or proportions for two categorical variables.
Simpson's paradox
A trend in groups that reverses when the groups are combined.

Visual diagram

Loading interactive diagram

Theory & deep dive

Univariate: centre, spread and shape

For a numeric variable, compute describe(), compare mean and median, and plot a histogram (try several bin widths) and a box plot. A mean well above the median signals right skew, often from money or durations; use the median, percentiles or a log scale. Look for multiple modes (mixtures of groups), spikes at round numbers (rounding or defaults), impossible values and gaps. Measures of spread (std, IQR) and percentiles (quantile([.01, .5, .99])) describe the tails.

For categorical variables, use value_counts(normalize=True) and a sorted bar chart; check cardinality, rare levels that should be grouped as "Other", and inconsistent spellings. Always include missing values in the counts (dropna=False) so you see how much information is absent. These checks drive decisions: log-transform skewed values, bin or cap extremes, merge rare categories.

Mean vs median tells a story

df.delivery_minutes.agg(["mean", "median"]) gives 38 and 31. The gap suggests a long right tail, so you plot a histogram on a log x-axis and report the median and 90th percentile (quantile(.9)) to describe typical and bad-day experience.

Key takeaway: Compare mean and median, plot the distribution, and treat categories by frequency and rarity.

Bivariate: relationships between pairs

Numeric vs numeric: scatter plots show direction, strength, curvature and clusters, with correlation as a one-number summary (next lesson), but a high correlation can hide a curve or outliers. Use hexbin plots or binned means (pd.cut then groupby(...).mean()) for big data. Category vs numeric: box plots or violins by group, plus a table of group means, medians and counts (df.groupby("city").delivery_minutes.agg(["median", "mean", "count"])). Category vs category: pd.crosstab(df.weather, df.late, normalize="index") gives the late rate within each weather condition.

Interpret carefully. A relationship may come from a third variable (confounding) or vanish when you split by group; Simpson's paradox is when a treatment looks worse overall but better within every subgroup because of unequal group sizes. Always check group sizes, avoid over-reading tiny groups and remember that association isn't causation. Use findings to choose features and to decide what to test next with a formal method.

Rate of late orders by weather

pd.crosstab(df.weather, df.is_late, normalize="index").round(2) shows 0.08 late in clear weather and 0.27 in rain. Check df.weather.value_counts() too: if rain has only 120 orders, the 27% is far less certain than the 8%.

Key takeaway: Choose the pair-appropriate plot and table, show group sizes, and beware confounders.

How it works, step by step

Step 1 of 4
Plan

1. Classify each variable

Mark columns as numeric, categorical, date or ID, and note the target if there is one.

delivery_minutes: numeric · weather: category
Key numbers at this step
Type
decides the plot

When to use it & trade-offs

What it is

Univariate analysis studies one variable at a time: its centre, spread, shape and unusual values (histogram, box plot, describe, value_counts). Bivariate analysis studies pairs: numeric vs numeric (scatter plot, correlation), category vs numeric (box or violin plots by group, group means), and category vs category (cross-tabs, stacked bars). Doing both before modelling tells you which transformations are needed, which variables look related to the target, and where the data is strange.

Why it matters: Models and averages hide problems that a simple plot makes obvious: a bimodal distribution that a mean describes badly, a long tail that dominates revenue, or a relationship that is curved rather than linear. Choosing the right summary and plot for each variable type is a basic skill that interviewers test with a table and a question like "what would you look at first?".

When to reach for it

  • Deciding transformations before modelling
  • Finding which variables relate to the target
  • Briefing stakeholders with a few clear comparisons

When to avoid or be careful

  • Reporting only the mean of a skewed variable
  • Comparing group rates without group sizes
  • Assuming a relationship is causal

Plots by variable combination

Histogram / box plot (one numeric)
Best for: Shape, centre, spread, outliers

Strengths: Reveals skew, modes and extremes.

Watch out: Bin width choice changes the picture.

Scatter / binned means (two numeric)
Best for: Direction, strength and curvature of relationships

Strengths: Shows what correlation hides.

Watch out: Overplotting on large data.

Group boxes and cross-tabs (with a category)
Best for: Comparing groups and category mixes

Strengths: Easy comparisons; reveals interactions.

Watch out: Small groups give unstable results.

Interview signals & numbers to know

When this topic comes up

  • “What would you plot first?”
  • “Mean is much higher than the median”
  • “How does X relate to Y?”
  • Simpson's paradox
  • Cross-tabulation

Benchmarks & rules of thumb

mean > median -
right-skew signal
p90 / p99 -
tail descriptions
n per group -
report beside every rate

Common mistakes & follow-ups

Common interview mistakes

  • Using the mean delivery time as the headline when a long tail makes it unrepresentative.
  • Comparing 27% late orders in rain with 8% in clear weather without noting rain has 120 orders.
  • Plotting a scatter of 5 million points as an unreadable black cloud.

Likely interviewer follow-ups

  • How do you choose a plot for category vs numeric?
  • What does a gap between mean and median tell you?
  • What is Simpson's paradox?
Interview tip: Always put counts next to group comparisons. A rate without its denominator is the most common way analyses mislead.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Univariate & Bivariate Analysis with data science interview Q&A and hands-on practice.

Open Data Science Q&A →Machine Learning Q&A
Previous topic
Exploratory Data Analysis (EDA) Workflows
Next topic
Correlation & Covariance Analysis