Part 4 of 12
NumPy and pandas Basics with a Bot: Cheat Sheet
Cheat sheetBy Bot Tutorials · Published · Facts last verified Nov 3, 2026
Part 4: shapes, dtypes, broadcasting, missing values and groupby, with real output from NumPy 2.5 and pandas 3.1, and prompts that keep a bot honest.
Almost every machine learning library sits on top of NumPy arrays, and most real data arrives as a pandas DataFrame. You don't need to know everything about them, but you do need a handful of ideas: shapes, dtypes, broadcasting, missing values and grouping. This cheat sheet covers those with a short script and its real output, and shows how to get a bot to explain your own data without making things up.
What the bot can and can't do here
It can:
- Explain why two arrays won't combine, given their shapes.
- Translate "I want the average price per part" into a pandas line.
- Read a traceback and find the column or shape at fault.
It can't:
- See your DataFrame. Paste
df.head(),df.dtypesanddf.shape, not a description. - Remember which defaults changed between versions. pandas 3 changed some, as the dtype line below shows.
- Decide what a missing value means in your data. That's your call.
Check first, transform second
Before you change data, look at it: shape, dtypes and missing counts. Most bugs are a wrong assumption about one of those three.
The script
import numpy as np, pandas as pd
rng = np.random.default_rng(42)
a = rng.normal(size=(3, 4))
print("shape", a.shape, "dtype", a.dtype)
print("column means", np.round(a.mean(axis=0), 3))
b = np.arange(4)
print("broadcast add row:", (a + b).shape)
df = pd.DataFrame({"part": ["gpu", "cpu", "ram", "gpu"], "price": [499.0, 219.0, None, 649.0]})
print(df.dtypes.to_dict())
print("missing prices:", int(df["price"].isna().sum()))
print(df.groupby("part")["price"].mean().round(1).to_dict())
print(df.dropna().query("price > 300")[["part", "price"]].to_string(index=False))
Real output on our box with NumPy 2.5.4 and pandas 3.1.0:
shape (3, 4) dtype float64
column means [-0.554 -1.065 0.586 0.467]
broadcast add row: (3, 4)
{'part': <StringDtype(storage='python', na_value=nan)>, 'price': dtype('float64')}
missing prices: 1
{'cpu': 219.0, 'gpu': 574.0, 'ram': nan}
part price
gpu 499.0
gpu 649.0
NumPy in five lines
- Shape and dtype first.
a.shapeis(3, 4): three rows, four columns.a.dtypeisfloat64, NumPy's default for these random numbers. - Axis means direction.
a.mean(axis=0)averages down the rows and gives one number per column, so four numbers. - Broadcasting. Adding
bwith shape(4,)toawith shape(3, 4)works because NumPy stretchesbacross each row. The result keeps shape(3, 4). If the last dimensions don't match, you get an error instead. - Random numbers with a Generator. NumPy's random docs recommend
np.random.default_rng(seed)for new code. A fixed seed makes the numbers repeatable on the same version. - Vectorize. Whole-array operations are usually much faster than Python loops. Part 11 measures this with a profiler.
pandas in five lines
- Check dtypes. On our box the
partcolumn came back as a pandasStringDtype, not the olderobjecttype, because we're on pandas 3.1. That's a good example of why bots that learned on older versions can be wrong about defaults. - Count missing values.
df["price"].isna().sum()gave 1. - Group and summarize.
groupby("part")["price"].mean()gave 574.0 forgpu, 219.0 forcpuandnanforram, because its only price was missing. - Drop or fill on purpose.
dropna()removed theramrow. Whether that's right depends on your data. - Filter with
query.query("price > 300")kept the two GPU rows.
Prompts that work
Here are
df.shape,df.dtypesanddf.head(10)(pasted). Write pandas code to get the mean ofpricefor eachpart. Don't assume columns I haven't shown. Tell me what happens to missing prices.These two arrays have shapes (3, 4) and (3,). Why does adding them fail, and what are two ways to fix it? Show the resulting shapes.
The second prompt is a good test of a bot. The right answer explains that broadcasting aligns trailing dimensions, so (3,) doesn't line up with the 4 columns, and suggests reshaping to (3, 1) or using the matching axis.
Common mistakes to watch for
- Chained assignment. Code like
df[df.x > 0]["y"] = 1may not changedf. Usedf.loc[mask, "y"] = 1. The pandas user guide explains indexing withloc. - Silent float conversion. An integer column with a missing value becomes float in many cases. Check dtypes after loading.
- Wrong axis. If a mean gives you the wrong number of values, the axis is probably swapped.
- Copying data into a bot. A sample of rows is usually enough. Don't paste private data.
Words you'll see
- ndarray: NumPy's array type. Every element has the same dtype.
- dtype: the data type, such as
float64,int64or a string type. - axis: the direction an operation runs. Axis 0 is down the rows, axis 1 across the columns.
- Series and DataFrame: pandas' one-column and table types. Each column of a DataFrame is a Series.
- NaN: "not a number", the usual marker for a missing float value.
Knowing these words makes bot answers easier to check, because you can ask exactly what you mean.
Tested vs documented
- Tested by us: every line of the script and every output shown, on NumPy 2.5.4 and pandas 3.1.0.
- Documented, not tested by us: the chained-assignment advice and the Generator recommendation, from the pandas and NumPy docs.
Where to go next
The next part today uses these arrays to train a first scikit-learn model.
Safety, licensing and privacy: read this before you run anything
This series is about learning and small personal projects with machine learning libraries. A bot is a helper, not an authority.
- Check bot advice against official docs. Version numbers, install commands and compatibility tables change often. Compare every answer with the PyTorch, TensorFlow, JAX, scikit-learn, NVIDIA, Hugging Face or ONNX Runtime pages we link. If the bot and the docs disagree, the docs win.
- Work in a virtual environment. Don't install or remove packages in your system Python, and don't run
sudo pip. - Run untrusted files carefully. Pickle-based model files can run code when loaded. Only load models from sources you trust, and prefer safer formats like safetensors where the library supports them.
- Respect licenses. Datasets and model weights carry their own licenses and terms. Read the model card and license before you use, share or sell anything built on them. We don't give legal advice; ask a qualified person if it matters.
- Protect personal data. Don't paste private, customer or medical data into a bot or a training set unless you have the right to, and check the bot's own privacy settings first.
- No hardware claims you didn't measure. Treat any speed figure from a bot as a guess until you've timed it on your own machine.
- No medical, legal or financial decisions from a toy model. These guides are for learning only.
FAQ
Why does the text column show a StringDtype?
That's what pandas 3.1 reported on our box for a column of strings. Older pandas versions showed object. If a bot tells you to expect object, check your version.
Should I use np.random.seed?
NumPy's docs recommend the Generator interface, created with np.random.default_rng, for new code. That's what our example uses.
Was this tested?
Yes. Every output on this page came from running the script on our box.
Sources
- NumPy: Installing NumPy
- NumPy: the absolute basics for beginners
- NumPy: Random Generator
- pandas: Installation
- pandas: 10 minutes to pandas
More in this series
- Part 1: GPU, Driver and CUDA Compatibility: A Bot-Assisted Check · Guide
- Part 2: How Much Memory Does a Model Need? A Data Brief · Data brief
- Part 3: Pin Versions and Build Reproducible ML Environments · Walkthrough
- Part 4: NumPy and pandas Basics with a Bot: Cheat Sheet (you are here) · Cheat sheet
- Part 5: Your First scikit-learn Model, Built with a Bot · Walkthrough
- Part 6: PyTorch on the CPU: First Steps with a Bot · Guide
- Part 7: TensorFlow and Keras Install Requirements, Explained · Explainer
- Part 8: JAX on the CPU: Field Notes with a Bot · Field notes
- Part 9: Read Hugging Face Model Cards and Licenses with a Bot · Guide
- Part 10: Export a Model to ONNX and Run It: A Deep Dive · Deep dive
- Part 11: Read ML Error Messages and Profile Code: Cheat Sheet · Cheat sheet
- Part 12: Review Your Training Code with a Bot: Checklist · Checklist
See the ML Libraries with a Bot series hub · Latest on Bot Tutorials
Bot advice can be wrong. We cite official docs; check them yourself before acting, especially for anything involving money, safety or your data.