Sub topic 3
Choosing Columns with `include` and `exclude`
Assume you know basic Python, have pandas imported as pd, and understand that a DataFrame is a table with labelled columns. This chapter focuses on targeted inspection after a first look at the whole table.
Choosing Columns with `include` and `exclude`
describe() summarizes numeric columns by default. Use include or exclude to choose other columns:
import pandas as pd
df = pd.DataFrame({
"city": ["Lima", "Oslo", "Lima", "Pune"],
"age": [34, 28, 41, 45],
"salary": [52000, 48000, 61000, 75000],
})
print(df.describe(include=["object"]))
print(df.describe(exclude=["object"]))
print(df.describe(include="all"))include=["object"] selects text columns. exclude=["object"] selects every column except text columns. include="all" requests summaries for every column, although some statistics become unavailable for particular dtypes.
Summarizing Text and Categorical Columns
For a text or categorical Series, describe() reports:
count: non-missing valuesunique: number of different valuestop: most frequent valuefreq: count of the most frequent value
Use df["city"].describe() when one column needs closer attention. A high number of unique cities may indicate an unexpectedly detailed field. A low count reveals missing city values.
Checking Memory Use with `info`
info() reports column names, non-null counts, dtypes, and memory use:
df.info()
df.info(memory_usage="deep")The deep option measures the contents of object columns more carefully. info() prints its result and returns None, so wrapping it in print() is unnecessary.
Spotting Missing Data, Wrong Dtypes, and Outliers
Use summaries as clues, not proof. A numeric column with a smaller count has missing values. An object column that should contain numbers may need conversion. Compare min, max, and quartiles in describe() to find extreme values. An unusually large maximum can be an outlier or a data-entry error.
| Signal | Possible problem |
|---|---|
| Numeric count below row count | Missing values |
| Number column has `object` dtype | Mixed or incorrectly parsed values |
| Maximum far above the 75th percentile | Possible outlier |
Putting It Together: A First-Look Routine
flowchart TD
A[head and tail] --> B[info]
B --> C[describe]
C --> D[Targeted include or exclude]
D --> E[Investigate suspicious columns]caption: A repeatable DataFrame inspection routine alt: A flowchart from previewing rows to targeted summaries and investigation.
Run head(), tail(), info(), and describe() first. Then inspect selected text columns with include=["object"] and individual columns with Series.describe(). Record suspicious missing counts, dtypes, and value ranges before cleaning the data.
Exercise
Which expression summarizes only text columns in a DataFrame?
Show answer
df.describe(include=["object"]).