What axis=0, axis=1 and axis=None mean in NumPy aggregations and operations
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Explain the `axis` argument in NumPy aggregations and operations. What do `axis=0`, `axis=1`, and `axis=None` mean, and how do they affect the result?
```hint Watch the shape
Write down the input shape and the output shape of `sum` on a `(2, 3)` array for each of the three settings.
```
### What a Strong Answer Covers
- What the axis number refers to and what happens to that dimension in a reduction
- The result shapes for a 2-D array under `axis=0`, `axis=1` and `axis=None`
- How `axis` behaves in operations that do not reduce, such as cumulative sums, sorting or concatenation
- Negative axes and `keepdims`, and why `keepdims` matters for broadcasting
### Follow-up Questions
- For an array of shape `(2, 3, 4)`, what shapes do `sum(axis=1)`, `sum(axis=-1)` and `sum(axis=(0, 2))` return?
- How would you subtract each row's mean from that row, and why does the obvious one-liner raise a broadcasting error?
- What does `argmax()` return when no axis is given, and how do you turn that into a row and column?
- In pandas, why does `df.drop("col", axis=1)` remove a column while `df.mean(axis=1)` returns one value per row?
Overview: Asks what the axis argument means in NumPy, how axis=0, axis=1 and axis=None change the result of an aggregation, and how it applies to operations that keep the array shape. Tests reasoning about array shapes, keepdims and broadcasting in everyday data work.