pandas.DataFrame.head#

DataFrame.head(n=5)[source]#

Return the first n rows.

This function exhibits the same behavior as df[:n], returning the first n rows based on position. It is useful for quickly checking if your object has the right type of data in it.

When n is positive, it returns the first n rows. For n equal to 0, it returns an empty object. When n is negative, it returns all rows except the last |n| rows, mirroring the behavior of df[:n].

If n is larger than the number of rows, this function returns all rows.

Parameters:
nint, default 5

Number of rows to select.

Returns:
same type as caller

The first n rows of the caller object.

See also

DataFrame.tail

Returns the last n rows.

Examples

>>> df = pd.DataFrame(
...     {
...         "animal": [
...             "alligator",
...             "bee",
...             "falcon",
...             "lion",
...             "monkey",
...             "parrot",
...             "shark",
...             "whale",
...             "zebra",
...         ]
...     }
... )
>>> df
      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
5     parrot
6      shark
7      whale
8      zebra

Viewing the first 5 lines

>>> df.head()
      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey

Viewing the first n lines (three in this case)

>>> df.head(3)
      animal
0  alligator
1        bee
2     falcon

For negative values of n

>>> df.head(-3)
      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
5     parrot