Module 10: NumPy and pandas
Work with data: NumPy arrays, pandas Series and DataFrames, grouping and charts.
- NumPy arrays: A NumPy array holds one type; arr 2, arr + arr and np.sqrt(arr) work on every element at once (vectorised); read shape, ndim, size, dtype.
- Working with arrays: m[r, c], m[:, c], m[r1:r2, c1:c2] slice 2D arrays; axis=0 gives per-column answers, axis=1 per-row; m[m 75] filters (use &, | with brackets); a slice is a view, so .copy() when you need independence.
- pandas Series: A Series is a labelled 1D array: make it from a list, dict, ndarray or scalar; loc uses labels (slice end included), iloc uses positions; maths lines up by label.
- DataFrames: A DataFrame is a table of Series sharing one index; df["col"] is a Series, df[["a", "b"]] a DataFrame; loc[rows, cols] by label, iloc by position; filter with df[mask] and &/|.
- Changing and summarising: Add columns with df["new"] = ...; rename, drop, sortvalues, fillna, dropna return new frames (assign back); df.groupby("key")["col"].sum() summarises; change cells with one df.loc[mask, col] = ....
- CSV in, charts out: pd.readcsv(path) loads a table (header=None if there is no header line); df.tocsv(path, index=False) saves one; plt.plot for trends, plt.bar for categories, plt.hist for spread, always with a title and axis labels.
Ends with a 13-question checkpoint (80% passes) and a mini project: Canteen Counter v2: the weekly sales report.