The 6 AI Engineering Patterns, come build with Greg live:Β Starts Jan 6th, 2025
Leverage
Pandas functions

Pandas Head – Preview Data – DataFrame.head()

Return top n rows of a Pandas DataFrame

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

Link to code

Official Documentation

On this page