Sub topic 4
Taking a First Look: shape, head, info, dtypes
This chapter assumes you can run Python in a notebook or script, have pandas installed, and know what a DataFrame is. You should already have train.csv in your working directory from the previous chapter. The goal is a quick, repeatable inspection that tells you what the table holds before you compute anything.
Why look before you compute
A first look answers four questions. How big is the table? What do the rows and columns look like? Does each column have the type you expect? Are values missing? Wrong answers here, such as a number stored as text, break later steps without raising an error. Spend two minutes on it every time.
The three core calls
import pandas as pd
df = pd.read_csv("train.csv")
print(df.shape)
print(df.head())
df.info()df.shape is a tuple of (rows, columns). For the Titanic training file it is (891, 12). It is an attribute, so you write it without parentheses.
df.head() shows the first five rows. Pass a number to change that, as in df.head(10). Use it to check that column names are sensible, that values sit in the right columns, and that nothing looks shifted or garbled. df.tail() shows the last rows, which can reveal problems such as a trailing summary line.
df.info() prints a summary and returns None. Do not wrap it in print(), because that adds a stray None to the output.
Reading the info output
Each line of info() lists a column, its non-null count, and its dtype. The non-null count is the number of values that are not missing. Compare it with the row count from shape.
| Column | What to notice |
|---|---|
| `Age` | Fewer non-null values than rows, so some ages are missing |
| `Cabin` | Far fewer non-null values than rows, so most cabins are missing |
| `Embarked` | Two values missing |
| `Survived`, `Pclass` | Complete, stored as integers |
You will measure these gaps properly in the next chapter. Here you only need to notice them.
Understanding dtypes
A dtype is the data type pandas uses to store a column. You can see them on their own:
print(df.dtypes)
print(df.dtypes.value_counts())Common dtypes you will meet:
int64: whole numbers, such asSurvivedandPclass.float64: decimal numbers, such asFare.Ageis also float, because a column of integers that contains missing values is stored as floats.object: usually text, such asName,Sex,Ticket,Cabin, andEmbarked.
Two details matter in practice. First, Survived and Pclass are stored as numbers, but they are really categories. Averaging Pclass is allowed by pandas but rarely meaningful. Second, a numeric column that shows up as object is a warning sign. It often means a stray string such as "N/A" or "?" is mixed in.
A compact first-look routine
The calls above fit in one reusable function:
def first_look(frame, rows=5):
print("shape:", frame.shape)
print()
print(frame.head(rows))
print()
frame.info()
first_look(df)Run it on every new dataset. The habit transfers to any table.
Common errors
- Writing
df.shape()raisesTypeError: 'tuple' object is not callable.shapeis an attribute. - Writing
print(df.info())printsNoneafter the summary. - Reading only
head()and assuming the whole file looks the same. The first rows may not represent the rest.
Exercise
After loading a CSV, df.shape is (500, 8). df.info() shows a column Price with 500 non-null values and dtype object. What does this combination suggest, and what would you check next?
Show answer
No values are missing, since the non-null count equals the row count. But object for a column that should be numeric suggests some entries are text, such as "$12" or "N/A". Check with df["Price"].head() and look at the unusual values, then convert it to a numeric type once you know what the stray strings are.