Inspect a dataset
Quickly preview imported or transformed data before deeper analysis.
Pandas · Function
Returns the first rows of a DataFrame by position. It defaults to five rows and supports positive, zero, and negative counts.
Call head on a DataFrame to obtain a positional preview. With the default, it returns five rows; pass an integer to change the count, including a negative value to omit rows from the end.
DataFrame.head(n=5)| Name | Type | Description |
|---|---|---|
n | int, default 5 | Number of rows to select. |
same type as caller: the first n rows of the caller object.
import pandas as pd
df = pd.DataFrame({"animal": ["alligator", "bee", "falcon", "lion", "monkey", "parrot"]})
print(df.head(3))
print(df.head(-2))The first call previews three rows; the second returns every row except the final two.
Quickly preview imported or transformed data before deeper analysis.
Check whether columns contain the expected values and types during development.
Display all but a specified number of trailing records when using a negative count.
DataFrame.head() return by default?DataFrame.head returns the first n rows based on position, with n defaulting to 5. It returns the same type as the caller.
Follow-up: What does a negative n do? It returns all rows except the last |n| rows, matching positional slicing such as df[:n].
n parameter?The argument is an integer specifying how many rows to select. Omitting it uses the default value of 5.
Follow-up: What happens when the integer is larger than the row count? The complete DataFrame is returned.
head commonly used during data exploration?The method is useful for quickly checking whether a DataFrame contains the expected kind of data. It selects rows by position rather than by label.
Follow-up: How can you inspect no rows while preserving the DataFrame type? Pass n=0, which returns an empty object.
DataFrame.tailDataFrame.ilocDataFrame.loc