Pandas Head might be our single most used function in the library. It is essential for quickly testing to ensure your data is correct, especially with a notebook type environment.
Pandas Head will return the top n-number of rows of your DataFrame. It is essential for checking to make sure your data is what you think it is. It’s close cousin, Pandas Tail will return the bottom n-number of rows.
1. pd.DataFrame.head(n=number_of_rows)
Pseudo code: Return the top n-rows of a Pandas DataFrame.
Pandas Head

Head Parameters
.head()
only has one parameter and it’s super easy: How many rows do you want to preview?
- n (Default=5): The number of rows you’d like to preview. By default Pandas will show you 5 rows. I most often use the default, but occasionally I’ll only do n=1 or n=10.
Pandas Tail is the exact same thing as Pandas Head but you’ll be returned with the last rows of a DataFrame instead of first rows.
If you wanted a random sample of N rows, then check out Pandas Sample.
Let’s look at an example
import pandas as pd
import numpy as np
Pandas Head¶
Pandas Head is pretty straight forward: It's a great way to view the first N rows of your DataFrame. Let's check out how to use .head() and a few other ways to pull rows from your DataFrame: .tail() and .sample()
Examples:
- Pandas Head - Default on DataFrame + Series
- Pandas Head - Custom N Rows
- Pulling Rows from your DataFrame: .tail() and .sample()
But first, let's create a tall DataFrame
np.random.seed(seed=42)
num_students = 1000
df = pd.DataFrame(data=np.random.randint(0,10,(num_students,3)),
columns=('Score1', 'Score2', 'Score3'),
index=["Student{}".format(x) for x in range(1, num_students+1)])
print ("Your DataFrame is {:,} rows long".format(len(df)))
1. Pandas Head - Default¶
Let's check out the top rows of our dataset. In order to do this we need to call .head() on our DataFrame. You can do this both on a DataFrame and a Series.
Notice how Pandas will return 5 rows with .head() by default.
df.head()
df['Score2'].head()
2. Pandas Head - Custom N Rows¶
If you wanted to see the first N rows of your DataFrame then all you need to do is pass in an integer into .head().
Here I want to see the first 3 rows of my DataFrame. You can either specify n=3, or simply pass in 3.
df.head(n=3)
df.head(3)
df.tail()
df.tail(2)
.sample() will return a random sample of rows. By default you'll only get 1 sample, but you can also specify N rows.
df.sample()
df.sample(3)
Check out more Pandas functions on our Pandas Page