Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pandas · Function

df.head

Returns the first rows of a DataFrame by position. It defaults to five rows and supports positive, zero, and negative counts.

How it works

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.

Syntax

DataFrame.head(n=5)

Parameters

NameTypeDescription
nint, default 5Number of rows to select.

Returns

same type as caller: the first n rows of the caller object.

Key points

  • Rows are selected positionally, with behavior equivalent to the corresponding slice.
  • A positive value selects that many rows from the beginning.
  • A negative value excludes the specified number of rows from the end.
  • The integer argument controls the requested preview size.
  • The result is a new view of the caller's leading rows, retaining the caller's object type.

Common mistakes

  • Passing zero produces an empty DataFrame rather than the first row.
  • A request beyond the DataFrame's length is capped at the available rows.

Example: Preview selected rows

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.

When to use it

Inspect a dataset

Quickly preview imported or transformed data before deeper analysis.

Validate data shape

Check whether columns contain the expected values and types during development.

Exclude trailing rows

Display all but a specified number of trailing records when using a negative count.

Interview questions

QWhat does 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].

QWhat is the purpose of the 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.

QWhy is 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.tail
  • DataFrame.iloc
  • DataFrame.loc

Source: pandas.DataFrame.head. Checked 2026-10-15.