In pandas, what do shape, dtypes, head(), info() and describe() each tell you about a DataFrame you have just built?
answer
- size, types, a sample, a summary
- an attribute versus methods
- non-null count against entries
- info prints and returns None
- describe skips non-numeric by default
basics
~20 sshape gives (rows, columns); dtypes gives each column's type; head() shows the first five rows; info() prints the index, each column's non-null count and dtype; describe() gives count, mean, spread and quartiles of numeric columns unless include='all'.
solid answer
~40 s`df.shape` is an attribute holding the `(rows, columns)` tuple. `df.dtypes` is a Series mapping each column to its dtype; in pandas 3.0 text columns show the `str` dtype rather than `object`. `df.head()` returns the first five rows (`head(n)` for another count, `tail()` for the end). `df.info()` **prints** a report and returns `None`: the index type and range, then per column the `Non-Null Count` and `Dtype`, a dtype tally and a memory line, so `2 non-null` against `3 entries` locates missing values. `df.describe()` returns a DataFrame of `count`, `mean`, `std`, `min`, the 25/50/75 percentiles and `max` for numeric columns only; `include="all"` adds text columns with `count`, `unique`, `top` and `freq`.
code
python · 13 linesimport pandas as pd
orders = pd.DataFrame(
[
{"order_id": "A1", "qty": 2, "price": 9.5},
{"order_id": "A2", "qty": 1, "price": 4.0, "coupon": "SPRING"},
{"order_id": "A3", "price": 12.0},
]
)
print(orders.shape)
result = orders.info()
print(result)
print(orders.describe().loc[["count", "mean", "max"]])go deeper
Recall what each of the five tools shows, that shape is an attribute, and that info() prints rather than returns.
Explain how to read non-null counts against entries, why describe() skips non-numeric columns, and what include='all' adds.
Use info() and describe() together to find columns that arrived as the wrong type, and know pandas 3.0 shows text as str.
Decide which inspection checks a shared pipeline should log at load time so that type drift is caught before analysis.
## Why inspect right after building A DataFrame constructor rarely fails: missing keys become missing values, mixed values become a general-purpose column, and integers with gaps become floats. The five inspection tools are how you find out what the constructor actually inferred before any transformation trusts it. Each answers a different question. | Tool | Kind | Answers | Returns | |---|---|---|---| | `df.shape` | attribute | how big is it? | tuple `(rows, columns)` | | `df.dtypes` | attribute | what type is each column? | Series of dtypes | | `df.head(n=5)` | method | what do the values look like? | DataFrame of the first n rows | | `df.info()` | method | how complete is each column, and what type? | `None` (prints a report) | | `df.describe()` | method | how are the numbers distributed? | DataFrame of statistics | ## shape, dtypes and head - `df.shape` is an **attribute**, so you write `df.shape`, not `df.shape()`. It confirms the row count against what the source promised, such as the number of records an API returned. - `df.dtypes` lists one **dtype** (data type) per column: `int64`, `float64`, `bool`, `datetime64[...]`, `object`, and **in pandas 3.0** `str` for text. A numeric field showing `float64` when you expected integers, or `object` when you expected numbers, is the first sign of dirty input. - `df.head()` returns the first five rows and `df.tail()` the last five; both take `n`, and a negative `n` means "all but": `head(-2)` returns every row except the last two. ## Reading info() `info()` prints a concise report to standard output and **returns `None`**, so `summary = df.info()` stores nothing useful; pass `buf=` a writable buffer to capture the text. The report contains: 1. the class name and the index, for example `RangeIndex: 3 entries, 0 to 2`, which gives the row count; 2. one line per column with its position, name, **`Non-Null Count`** and **`Dtype`**; 3. a tally such as `dtypes: float64(2), str(2)`; 4. a memory usage line, whose meaning and deep variant belong to the lesson on speed and memory. The column lines carry the diagnosis. A column whose non-null count is below the entry count has missing values, and its dtype tells you what pandas did about them. For frames with more than `display.max_info_columns` columns (100 by default), `info()` switches to a short summary; `verbose=True` forces the full listing. Non-null counts are shown only when the frame is smaller than both the `display.max_info_rows` and `display.max_info_columns` options, and `show_counts=True` forces them. ## Reading describe() `describe()` returns a DataFrame of summary statistics, excluding missing values from each calculation: - by default it covers **only numeric columns**, with `count`, `mean`, `std`, `min`, `25%`, `50%`, `75%` and `max` (the 50% row is the median); - `percentiles=[0.05, 0.95]` changes which quantiles appear; - `include="all"` covers every column and adds `unique`, `top` (most common value) and `freq` (its count) for text, leaving `NaN` where a statistic does not apply; - `include=` and `exclude=` also accept dtype lists, such as `include=["number"]`; - if a frame has no numeric columns at all, the default describes the text and categorical columns instead. Two readings matter in an interview. A numeric field that is **missing from describe()** is not numeric: it probably arrived as text or mixed values. A `count` row lower than `shape[0]` means missing values in that column. ## What changed in pandas 3.0 In pandas 2.x a column of Python strings had dtype `object`, the same as a column mixing strings with numbers or dicts. **In pandas 3.0** text infers the dedicated `str` dtype, which holds only strings or missing values (shown as `NaN`), so `dtypes` and `info()` print `str` for a clean text column. That makes the report more informative: `object` now stands out as "something other than plain text", which the companion question on the sensor export works through. Code that checks `df[col].dtype == object` to find text columns needs updating, which the lesson on text columns covers. ## A routine worth stating 1. `df.shape` to confirm row and column counts. 2. `df.info()` to read dtypes and non-null counts in one pass. 3. `df.describe()` (and `include="all"` for text) to sanity-check ranges. 4. `df.head()` to eyeball a few real values.
- How do you capture info() output as text, for a log line or a test?Pass a writable buffer: create `buf = io.StringIO()`, call `df.info(buf=buf)` and read `buf.getvalue()`. The method still returns `None`; the buffer receives what would have gone to standard output.
- Why might a numeric-looking column not appear in describe() at all?By default `describe()` covers only numeric dtypes. If the column holds text or a mix of numbers and strings, its dtype is `str` or `object` and it is skipped. Checking `df.dtypes` or calling `describe(include="all")` shows it with `count`, `unique`, `top` and `freq` instead.
- When does info() stop printing non-null counts?When the frame reaches the `display.max_info_rows` or `display.max_info_columns` option thresholds, counting every column becomes expensive and the counts are omitted by default. `df.info(show_counts=True)` forces them, and `verbose=True` forces one line per column on very wide frames.
- df.shape() is a method that returns the row count
- info() returns a DataFrame you can filter
- describe() summarises every column by default
- Text columns show dtype object in pandas 3.0
- head() returns the first ten rows by default
Assumes pandas 3.0: 2.x showed text columns as object in dtypes and info(); 3.0 infers and shows the str dtype.