Sub topic 8
Choosing columns with include and exclude
This chapter assumes you can run Python code, have pandas imported as pd, and know that a DataFrame is a table of rows and labelled columns. You should also have seen describe() summarize numeric columns. Here you control which columns it summarizes.
Why the default is not enough
By default, DataFrame.describe() reports only numeric columns. In the example table below, that means age and salary. The text columns name and city are silently left out, so a quick look can hide problems in them, such as unexpected categories.
The include and exclude arguments let you choose columns by dtype. A dtype is the data type pandas stores for a column.
import numpy as np
import pandas as pd
df = pd.DataFrame({
"name": ["Ana", "Ben", "Cleo", "Dev", "Eli"],
"age": [34, 28, np.nan, 45, 39],
"city": ["Lima", "Oslo", "Lima", "Pune", "Oslo"],
"salary": [52000, 48000, 61000, 75000, 58000],
})
print(df.describe())The output has two columns, age and salary, with eight statistic rows: count, mean, std, min, 25%, 50%, 75%, and max.
Summarizing every column with include="all"
Pass include="all" to get every column in one table.
print(df.describe(include="all"))The result has the union of the statistic rows for both kinds of column: count, unique, top, and freq for text, plus mean, std, min, the quartiles, and max for numbers. Each column fills only the rows that make sense for it. The rest show NaN. For example, age has NaN under unique, top, and freq, and city has NaN under mean.
For city, unique is 3 and freq is 2. top is a value that appears twice, either Lima or Oslo. When values tie, pandas picks one of them, so do not rely on which.
Selecting dtypes with include
include also accepts a dtype name or a list of names. Common choices are:
| Argument | Columns summarized |
|---|---|
| `include="number"` | Integer and float columns |
| `include=["number"]` | Same as above, written as a list |
| `include="object"` | Columns stored as Python objects, usually text |
| `include="all"` | Every column |
print(df.describe(include="number"))
print(df.describe(include=["object"]))The second call works when text is stored as object, which is the default in most pandas versions. Newer pandas versions can store text in a dedicated string dtype. A column with that dtype will not match "object", and you will see this as a missing column or an error. That makes exclude a safer way to select text.
Dropping dtypes with exclude
exclude removes dtypes from the summary. Excluding numbers is a reliable way to get only the non-numeric columns, whatever dtype holds the text.
print(df.describe(exclude="number"))This summarizes name and city with count, unique, top, and freq. Both arguments accept a list, so exclude=["number", "object"] removes both kinds.
Choosing between the options
The decision depends on what you want to inspect.
Things to remember
includeandexcludeapply toDataFrame.describe(). A Series has one dtype, soSeries.describe()needs no column selection.- Mixed summaries are wide and full of
NaN. When you want to compare numeric columns, calldescribe()with the default, and read text columns in a separate call. - Result tables are DataFrames. You can transpose one with
.Tto put columns down the side, which is easier to read when a frame has many columns.
print(df.describe(include="all").T)Exercise
You call df.describe(include="all") on the example DataFrame. Why does the age column show NaN in the unique, top, and freq rows, and how would you get a summary of only the non-numeric columns?
Show answer
Those rows are statistics for text-like data, and age is numeric, so pandas leaves them as NaN for that column. To summarize only the non-numeric columns, call df.describe(exclude="number"). This keeps working even if text is stored in a string dtype instead of object.