Sub topic 3
Relationships Between Pairs of Variables
Assume you know basic Python, pandas DataFrames, Jupyter cells, and introductory statistics. Bivariate analysis examines how two variables relate. The relationship may be a trend, a difference between groups, or no clear pattern.
Compare Two Variables
Choose a plot that matches the variable types:
- Two numeric variables: use a scatter plot.
- One numeric and one categorical variable: use grouped box plots.
- Two categorical variables: compare counts with a grouped bar chart.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame({
"region": ["east", "east", "west", "west", "north", "north"],
"quantity": [2, 4, 3, 8, 5, 6],
"amount": [20, 45, 30, 90, 55, 70],
})
fig, ax = plt.subplots(figsize=(6, 4))
for region, group in df.groupby("region"):
ax.scatter(group["quantity"], group["amount"], label=region)
ax.set_title("Order amount and quantity")
ax.set_xlabel("Quantity")
ax.set_ylabel("Amount")
ax.legend(title="Region")
plt.tight_layout()
plt.show()A visible pattern is a prompt for investigation, not proof of cause.
Record Findings Beside the Evidence
Place a short Markdown cell directly after each plot. Include the observation, the evidence, and an open question.
- Finding: Larger quantities usually have larger amounts.
- Evidence: The points rise from left to right, but the sample is small.
- Open question: Does this pattern remain after separating regions?
This structure prevents conclusions from becoming disconnected from the code that produced them.
Pitfalls and Information Leakage
Correlation does not establish causation. A third variable may explain both observed variables. Small groups can also create unstable patterns.
Information leakage occurs when analysis uses information that would not be available at the intended decision time. For example, using a later refund status to explain the original order amount can make a relationship look stronger than it would be in production.
Remove Hidden Notebook State
A notebook may work only because an earlier cell created a variable. Verify it by restarting the kernel and choosing Run All. Fix cells that depend on execution order, stale variables, or manual edits.
Move Stable Code into Modules
Once cleaning or reporting logic stops changing, move it into a Python module such as analysis.py. Keep the notebook for choices, plots, findings, and narrative.
# analysis.py
def summarize_by_region(df):
return df.groupby("region", as_index=False)["amount"].agg(
orders="size",
median_amount="median",
)Import and test this function from the notebook.
Hand Off the Results
End with a concise summary:
- questions investigated;
- data and cleaning rules used;
- main findings and their limitations;
- unanswered questions;
- paths to the notebook and stable modules.
Exercise
Why should a finding appear immediately after the plot that supports it?
Show answer
The nearby evidence makes the reasoning traceable and separates observed facts from later assumptions.