ertac.paprat.com
EN

← Writing

Python for Data Science: One CSV, Three Libraries, One Question

· 2 min read · English

Rewritten: . Rewritten with AI assistance. Examples and tool references follow the original publication period.

The question is small: did deliveries from warehouse B take longer than deliveries from warehouse A?

That does not call for installing every popular data-science library. It calls for reading a table, calculating a few quantities, and looking at the observations behind the averages.

The example below uses invented data so that the entire analysis fits on the page. It assumes a Python environment with pandas, NumPy, and Matplotlib installed. These APIs were available by February 2024.

from io import StringIO

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

csv = StringIO("""warehouse,hours
A,20
A,22
A,24
B,20
B,22
B,60
""")

deliveries = pd.read_csv(csv)
groups = ["A", "B"]

print(deliveries.groupby("warehouse")["hours"].agg(
    ["count", "mean", "median"]
))

for warehouse in groups:
    hours = deliveries.loc[
        deliveries["warehouse"] == warehouse, "hours"
    ].to_numpy()
    deviations = np.abs(hours - np.median(hours))
    print(warehouse, "median absolute deviation:", np.median(deviations))

fig, ax = plt.subplots()
for position, warehouse in enumerate(groups):
    hours = deliveries.loc[
        deliveries["warehouse"] == warehouse, "hours"
    ]
    ax.scatter([position] * len(hours), hours, s=60)

ax.set_xticks(range(len(groups)))
ax.set_xticklabels(groups)
ax.set_xlabel("Warehouse")
ax.set_ylabel("Delivery duration (hours)")
ax.set_title("Six illustrative deliveries")
fig.tight_layout()
fig.savefig("delivery-durations.png", dpi=150)
plt.close(fig)

Let each library do a recognizable job

Pandas reads the CSV, holds the labeled table, and groups records by warehouse. The printed means are 22 hours for A and 34 hours for B. Both medians are 22 hours.

NumPy operates on the numerical arrays. Here it calculates absolute deviations from each group’s median. Both groups have a median absolute deviation of 2 hours. That deliberately small calculation shows why a robust summary can miss a rare but operationally important event: the 60-hour delivery does not move either group’s median or median absolute deviation in this sample.

Matplotlib makes the individual observations visible. With only six points, a scatter plot is more revealing than a polished dashboard. You can see exactly which observation separates the averages.

Pandas could handle more of these calculations itself. The purpose of using three libraries here is to show their roles, not to insist that every analysis needs all three in its import list.

The output does not settle the question

Warehouse B’s sample mean is higher. That is a description of these six records. It does not establish that B generally operates more slowly, or that the warehouse caused the delay.

We would want more observations and information about destination, shipping service, order time, and what happened to the 60-hour delivery. Perhaps B handles distant routes. Perhaps one package was mis-scanned. Perhaps the long delivery reflects a recurring failure that a median conceals.

Do not delete it merely to make the groups look similar. The reason for an unusual value belongs in the investigation.

Add a model when the question changes

If the task becomes predicting duration for new orders, scikit-learn can provide preprocessing, estimators, and evaluation tools. That introduces new obligations: define the prediction moment, split the data appropriately, and compare against a baseline.

Deep-learning frameworks serve other needs; they are not a mandatory next step after plotting a CSV. A useful Python toolkit grows with the questions you can formulate and test. For this question, the next valuable action is probably to inspect a shipment record, not import another package.