4.3 · Data science: NumPy, pandas, Matplotlib

Level 4 · Specializations (Web, Data, AI, Automation)

4.3Data science: NumPy, pandas, Matplotlib

Goal: Analyse data with pandas (inspect, filter, aggregate) and show it with Matplotlib — to answer a business question in 5 lines instead of a spreadsheet day.
Estimated time: 15 min · self-paced reading · If you only have 5 min: read “The essentials in 30s” + the diagram.

The essentials in 30 seconds

  • The trio: NumPy computes fast (vectorised arrays), pandas handles tables (DataFrame), Matplotlib shows (1 chart = 1 message).
  • Nabil's method: inspect first (head/info/describe/isna) then aggregate (groupby + sort = 80% of reports) — never the reverse.
  • Vectorise, never loop: (df.montant * 1.2).mean() replaces loops, 100x faster on one million rows.

Running mini-case — Nabil answers in 5 lines

Management asks Nabil, in Algiers: “what are our 5 best cities this quarter?” Instead of a day sorting in a spreadsheet, Nabil loads the CSV with pandas, inspects (head, info, isna for gaps), aggregates by city with groupby and sort, then draws a titled bar chart. Answer delivered in one hour, reproducible next quarter by rerunning the script. Third milestone of the running thread: the analyst who answers fast, and proves what he claims.

Sequel to cases 4.1-4.2 — Nabil analyses the sales he exposes.

1. NumPy: compute fast, pandas: handle tables

NumPy replaces loops with vectorised arrays computed in C: (a * 1.2).mean() processes one million rows at once — 100x faster for ÷10 lines of code. pandas is the programmable spreadsheet: the DataFrame (rows/columns table) loads (read_csv), inspects (head, info, describe, isna), filters (df[df.ville == "Alger"]), aggregates (groupby(...).agg(...)) and merges (merge). Nabil ALWAYS inspects before analysing: types, missing values and orders of magnitude first — cleaning is the longest and most important step.

2. Matplotlib: show, and the Jupyter notebook

Matplotlib turns figures into a message: 1 chart = 1 message, with a title stating the conclusion (“Algiers drives 40% of revenue” rather than “Revenue by city”). df.plot() is enough to explore; a polished figure (title, labels, source) serves to communicate. The Jupyter notebook mixes code, results and narrative: ideal to explore and tell the analysis step by step. Nabil remembers the rule: pandas replaces the spreadsheet for large volumes and reproducibility; the spreadsheet stays perfect for quick small visual volumes.

Common misinterpretation: “More charts = better analysis.” No. Ten charts with no message drown the conclusion three well-chosen figures would have carried. Analysis means answering ONE question with minimal proof; the chart is only the last step — showing — after inspecting and aggregating.
The 4-step analysis method Load → inspect → aggregate → show Load read_csv: the CSVbecomes a DataFrameInspect head, info,describe, isna (gaps)Aggregate groupby + sort:the figured answerShow 1 titled chart= 1 message
Load → inspect → aggregate → show
Text version of the diagram (SEO / screen readers / translation): 1) Nabil loads the CSV into a DataFrame with read_csv. 2) He inspects: head, info, describe, isna to spot gaps and types. 3) He aggregates with groupby and sort to get the answer. 4) He draws a chart with a message-title. Lesson: inspect before analysing, vectorise instead of looping, show one message per chart.

Did you know?

pandas owes its name to “panel data”, an econometrics term — not to the bear.
True: Nabil's favourite tool is an economist's nod.

The bear is only the data analysts' unofficial mascot.

🔎 Concrete example — 🔎 Concrete example — Nabil's top cities

Nabil's complete analysis in 5 lines:

df = pd.read_csv("ventes.csv")
print(df.info(), df.isna().sum())      # inspect first
top = df.groupby("ville")["montant"].sum().sort_values(ascending=False)
print(top.head())                          # the figured answer
top.head().plot(kind="bar", title="Revenue by city")

Reading: inspect → aggregate → show, in that order.

Lesson: groupby + sort = 80% of activity reports.

Section vocabulary

NumPy
Computing library on vectorised arrays (C operations): replaces Python loops, 100x faster on large volumes.

Simple example: Nabil applies a discount to one million amounts in one line.

What it is not: a for loop processing rows one by one.

Reflex: as soon as you loop over numbers, look for the vectorised operation.

pandas / DataFrame
Programmable spreadsheet: the DataFrame (rows/columns table) loads, filters, aggregates and merges data — read_csv, groupby, merge.

Simple example: Nabil aggregates his sales by city in two lines.

What it is not: manual sorts repeated in a spreadsheet.

Reflex: inspect (head/info/isna) before any analysis.

groupby + aggregation
Grouping rows by category then summarising (sum, mean): the move producing 80% of reports.

Simple example: Nabil's revenue by city: groupby city, sum of amounts, sort.

What it is not: totals recalculated by hand at each request.

Reflex: question by category? groupby first.

Matplotlib
Charting library: 1 chart = 1 message, with a title stating the conclusion.

Simple example: Nabil titles his bar chart with the conclusion, not just “Revenue by city”.

What it is not: ten decorative charts with no message.

Reflex: message-title, labels, source — otherwise it is decoration.

In practice — In practice — Analyse a CSV end to end

  1. Load a CSV with pd.read_csv and inspect: head, info, describe, isna().sum() — note types and gaps.
  2. Clean the vital minimum: date/amount types, missing values flagged or handled.
  3. Filter (df[condition]) then aggregate (groupby + agg + sort) to answer ONE precise question.
  4. Draw 1 chart with a message-title, then rerun the whole script at once to prove it is reproducible.
You answer a business question with a reproducible script: inspected, aggregated, shown — in minutes instead of a day.

Ready-to-say script — presenting your analysis (30 s)

“This is Nabil: I loaded the sales, inspected gaps and types, then aggregated by city — Algiers drives most of the quarter. The script reruns identically next quarter, and the chart states the conclusion in its title.”

Say in a business review; show the chart, not the code.

Copy-paste template — pandas analysis (tested code)

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("ventes.csv")

# 1. Inspect first: types, gaps, orders of magnitude
print(df.head())
print(df.info())
print(df.describe())
print(df.isna().sum())

# 2. Filter: Algiers sales this quarter

alger = df[df["ville"] == "Alger"]

# 3. Aggregate: revenue by city, sorted (80% of reports)
top = df.groupby("ville")["montant"].sum().sort_values(ascending=False)
print(top.head())

# 4. Show: 1 chart = 1 message
top.head().plot(kind="bar", title="Algiers drives most revenue")
plt.tight_layout()
plt.savefig("top-villes.png")

Small file

A few hundred rows? The spreadsheet stays faster — keep pandas for reproducibility.

Monthly report

Like Nabil: one script loading, inspecting, aggregating and exporting — rerun each month untouched.

Million rows

Vectorise everything (never for over rows): NumPy/pandas in C do in seconds what loops do in minutes.

Free exploration

Work in a Jupyter notebook: code, results and narrative side by side to search before concluding.

Caution: Figures must be verified: always inspect before concluding (one gap or wrong type invalidates everything) and only share anonymised data outside the authorised circle. Educational content, examples with fictitious data.

✓ Best practices

  • Inspect (head/info/describe/isna) before analysing.
  • Vectorise: never loop over millions of rows.
  • One chart = one message, concluding title.

✗ Common pitfalls

  • Looping with for over millions of rows.
  • Analysing without inspecting types and gaps.
  • Multiplying charts with no message.

Key takeaways

  • TRIO: NUMPY (vectorised computing) / PANDAS (tables) / MATPLOTLIB (charts).
  • DATAFRAME = programmable spreadsheet: read_csv, head, describe, groupby, merge.
  • METHOD: inspect → aggregate → show; cleaning = longest step.
  • JUPYTER = analysis narrative; 1 chart = 1 message with conclusion-title.

48h challenge — apply tomorrow

Take 1 real CSV from your work: inspect it, answer 1 question via groupby, and deliver 1 message-titled chart.

Section exit checklist
  • I can name NumPy, pandas/DataFrame, Matplotlib.
  • I can inspect then aggregate a CSV.
  • I have 1 reproducible analysis with 1 message-chart.
Self-rating 1-5: did I inspect before concluding?does my script rerun at once with no rework?

Frequently asked questions

Does pandas replace Excel?

For large volumes and reproducibility: yes — a script redo in seconds what a spreadsheet redoes in hours, with no copy-paste error. For quick small visual volumes: no — the spreadsheet stays unbeatable. Nabil uses both: pandas for monthly reports, the spreadsheet for a glance.

Do I need to be strong in maths for data science?

Not at this stage: basic statistics (mean, median, distribution) suffice to inspect and aggregate with pandas. Advanced maths (algebra, probability) becomes useful in 4.4 to choose and tune machine learning models.

Check your understanding

What is the core structure of pandas?

Additional resources (optional, to go further)

Level 4 examFresh, harder questions to test what you've learned