Practice: rainfall pipeline

Load, clean NaNs, broadcast revenue, rank cities, and export a numeric report.

Attach all five sample files (units.csv, prices.csv, rainfall.csv, temps.txt, gaps.csv) with Add files. Paste each block as the whole editor — later blocks repeat the load so they still run alone.

Row order is Nairobi, Mombasa, Kisumu, Nakuru, Eldoret. Columns of units.csv are products A, B, C.

Goal

Produce city_report.csv (revenue total + April rain) and revenue_grid.csv. Print Nairobi’s product shares.

1. Load and check shapes

import os

print("uploads:", os.listdir("/uploads"))
units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
rain = np.loadtxt("rainfall.csv", delimiter=",")
gaps = np.genfromtxt(
    "gaps.csv", delimiter=",", missing_values="nan", filling_values=np.nan
)
print("units", units.shape, "prices", prices.shape)
print("rain", rain.shape, "gaps", gaps.shape, "nan cells", np.isnan(gaps).sum())

2. Revenue by broadcasting

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
revenue = units * prices
print(revenue)
print()
print("city totals:", revenue.sum(axis=1))
print("product totals:", revenue.sum(axis=0))

3. Clean gappy rain

gaps = np.genfromtxt(
    "gaps.csv", delimiter=",", missing_values="nan", filling_values=np.nan
)
print("isnan before:", np.isnan(gaps).sum())
city_mean = np.nanmean(gaps, axis=1, keepdims=True)
filled = np.where(np.isnan(gaps), city_mean, gaps)
print("isnan after:", np.isnan(filled).sum())
print(filled)

4. Combine and rank

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
rain = np.loadtxt("rainfall.csv", delimiter=",")
revenue = units * prices
city_rev = revenue.sum(axis=1)
apr = rain[:, -1]
table = np.column_stack([city_rev, apr])
print(table)
print("highest revenue city index:", np.argmax(city_rev))
print("wettest April city index:", np.argmax(apr))
print("revenue rank (low → high):", np.argsort(city_rev))

Index 0 is Nairobi. Kisumu is often wettest in April in this sample.

5. Product shares

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
revenue = units * prices
shares = revenue / revenue.sum(axis=1, keepdims=True)
print("Nairobi shares (A, B, C):")
print(np.round(shares[0], 3))
print()
print(np.round(shares, 3))

6. Export the report

import os

units = np.loadtxt("units.csv", delimiter=",")
prices = np.loadtxt("prices.csv")
rain = np.loadtxt("rainfall.csv", delimiter=",")
revenue = units * prices
city_rev = revenue.sum(axis=1)
apr = rain[:, -1]
report = np.column_stack([city_rev, apr])
np.savetxt(
    "city_report.csv",
    report,
    delimiter=",",
    fmt="%.2f",
    header="revenue,apr_mm  rows: Nairobi, Mombasa, Kisumu, Nakuru, Eldoret",
)
np.savetxt(
    "revenue_grid.csv",
    revenue,
    delimiter=",",
    fmt="%.2f",
    header="A,B,C  rows: Nairobi, Mombasa, Kisumu, Nakuru, Eldoret",
)
print("uploads:", os.listdir("/uploads"))
print(open("/uploads/city_report.csv").read())

Click on city_report.csv and revenue_grid.csv.

Extra drills

  • np.where(rain > 150) — which city-months are that wet?
  • Eldoret minus Nairobi April rain: apr[4] - apr[0] after loading rainfall.csv.
  • np.partition(city_rev, -2)[-2:] — two highest revenue totals, unordered.
You should see

If units.csv is missing, attach the banner files and run again. FileNotFoundError means the editor cannot see the file in /uploads.