How to Use Pandas head() Function (With Examples)

How to Easily Preview DataFrames with Pandas head()

The Pandas library is the cornerstone of data manipulation and analysis in Python. A fundamental utility within this library is the head() function, an essential tool for initial data inspection. The head() function is engineered to quickly return the first n rows of a DataFrame, where ‘n’ represents an optional integer value. By default, if no argument is supplied, it displays the top five records. This functionality is absolutely crucial for quickly previewing data before intense data processing or analysis begins.

This function allows data scientists and analysts to instantly gain insight into the dataset’s basic structure, including column names, data types, and initial value formats. Without head(), one would have to print the entire dataset, which is inefficient and often impractical when dealing with millions of records. Mastering the application of this simple function significantly enhances the efficiency of your data workflow, enabling rapid data exploration and validation.


The Core Purpose and Syntax of head()

The primary objective of the head() function is to offer a snapshot preview of the DataFrame. When working with large datasets, it is imperative to verify that the data has been loaded correctly and that the structure matches expectations. Using head() provides this verification efficiently, ensuring the integrity of the initial loading process without consuming excessive memory or time.

You can use the head() function to view the first n rows of a Pandas DataFrame. The standard invocation requires only calling the method on the DataFrame object itself. If you omit the arguments, Pandas defaults to returning the top five rows, following standard conventions for rapid inspection.

This function uses the following basic syntax, which is straightforward and designed for simplicity in everyday data manipulation tasks:

df.head()

Understanding the output of head() is crucial. It returns a new DataFrame object containing the selected rows. This returned object maintains the original indexing, column names, and data types, providing an accurate representation of the start of the source data. This is particularly useful for quickly checking the index alignment, which is a common source of errors in complex data structures.

Setting Up the Sample DataFrame

To demonstrate the practical use of the head() function, we will first establish a sample DataFrame. This example dataset simulates performance metrics for a small group of athletes, including columns for ‘points’, ‘assists’, and ‘rebounds’. This dataset is intentionally small to allow for easy verification of the function’s output, but the principles demonstrated here scale seamlessly to datasets containing millions of observations.

The following examples show how to use this syntax in practice with the following Pandas DataFrame. Note that we first import the library using the standard alias pd, and then utilize the DataFrame constructor to structure our data. The creation process involves defining column names (keys) and their corresponding data lists (values).

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
df

	points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6
5	23	9	5
6	25	9	9
7	29	4	12

As observed in the output above, our DataFrame, named df, consists of eight rows and three columns, utilizing a default zero-based integer index. This setup will serve as the foundation for demonstrating the flexibility and power of the head() function across various scenarios in data exploration.

Example 1: Viewing the First 5 Rows (Default Behavior)

The most common application of the head() function is its default usage, which provides the top five rows of the DataFrame instantly. This is particularly useful immediately after loading a dataset from a file (like CSV or SQL), offering immediate confirmation that the data structure is sound and the data itself has been parsed correctly.

By default, the head() function displays the first five rows of a DataFrame. This default setting is widely adopted across many statistical software packages and libraries because five rows offer enough visibility to identify common issues such as incorrect delimiters, missing header rows, or unexpected character encoding errors, without overwhelming the user interface.

#view first five rows of DataFrame
df.head()

	points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10
3	14	9	6
4	19	12	6

The resulting output confirms that rows with index 0 through 4 have been successfully extracted. Notice that the column headers and the original index values are preserved. If your DataFrame has fewer than five rows, head() will simply return all existing rows. It gracefully handles smaller datasets, ensuring it never throws an error due to insufficient data length.

Example 2: Customizing Output with the ‘n’ Argument

While the default five-row view is often sufficient, there are many scenarios where you need to examine a specific number of records. For instance, if you are conducting an audit of a specific transaction batch, you might need to view the first 10 or 20 records to confirm a specific sequence of operations. This is where the flexibility of the ‘n’ argument becomes indispensable.

We can use the n argument to view the first n rows of a Pandas DataFrame. This parameter accepts any positive integer. If the provided integer n exceeds the total number of rows in the DataFrame, head(n) will return the entire DataFrame without raising an exception, maintaining robust error handling.

#view first three rows of DataFrame
df.head(n=3)

        points	assists	rebounds
0	25	5	11
1	12	7	8
2	15	7	10

In the example above, by setting n=3, the function returns only the first three records (indices 0, 1, and 2). This demonstrates precise control over the data preview, allowing analysts to tailor the output strictly based on their immediate requirements. Using a smaller n is particularly helpful when working in environments with limited screen space or when focusing on very specific patterns at the beginning of a time series data structure.

Example 3: Applying head() to a Specific Column (Series)

The head() function is not exclusive to DataFrames; it is also a powerful method available to Pandas Series objects, which are essentially single columns of a DataFrame. Viewing the head of a specific column is critical when you need to quickly assess the data type or value format integrity of a single variable, such as ensuring a numeric column doesn’t contain unexpected string values.

To apply head() to a column, you first select the desired column using standard bracket notation (e.g., df['column_name']). This selection returns a Series object, upon which the head() function can be invoked directly. The following code shows how to view the first five rows of a specific column in a DataFrame, focusing on the ‘points’ variable.

#view first five rows of values in 'points' column
df['points'].head()

0    25
1    12
2    15
3    14
4    19
Name: points, dtype: int64

The resulting output is a Series containing the index, the values, and important metadata like the Series name (‘points’) and its data type (dtype: int64). This approach is highly efficient for validating the consistency of a single feature without rendering the potentially dozens of other columns present in the full DataFrame, streamlining the initial phase of data quality checks during data processing.

Example 4: Viewing the Start of Subsets (Multiple Columns)

Often, data exploration requires inspecting the relationship between a small group of columns rather than the entire dataset or a single column. The head() function can be seamlessly applied to subsets of a DataFrame, allowing analysts to focus their inspection on correlated variables, such as ‘points’ and ‘assists’ in our sports performance dataset.

To achieve this, we use the double-bracket notation (e.g., df[['col1', 'col2']]) to select multiple columns. This selection returns a new DataFrame object, preserving the specified columns and all rows. Subsequently, calling head() on this new subsetted DataFrame provides a targeted preview. The following code shows how to view the first five rows of several specific columns in a DataFrame, isolating ‘points’ and ‘assists’.

#view first five rows of values in 'points' and 'assists' columns
df[['points', 'assists']].head()

	points	assists
0	25	5
1	12	7
2	15	7
3	14	9
4	19	12

This targeted approach is highly effective in initial statistical analysis, where you might only be concerned with a few key features. It keeps the workspace clean and focused, reducing visual clutter and speeding up the process of identifying potential outliers or anomalies within the variables of interest before committing to more resource-intensive computations.

Best Practices and Use Cases for head()

Integrating head() effectively into your workflow is a hallmark of efficient data analysis. The function is invaluable not just for viewing the start of the data but also as a debugging tool. When performing transformations or calculations on a DataFrame, periodically calling df.head() after each major step confirms that the transformation executed as intended on the leading rows, providing immediate feedback on code correctness.

One common use case is verifying the impact of filtering operations. For instance, after applying a complex boolean mask to filter a DataFrame based on certain conditions, using head() immediately verifies that the resulting DataFrame contains only the expected records. This preventative check saves significant time down the line by catching logical errors early in the data analysis pipeline.

Furthermore, head() is crucial when dealing with external data sources. When reading data from streaming APIs or network connections, the initial rows might contain headers, metadata, or corrupted entries. A quick check using head(10) allows the analyst to detect these issues instantly and implement necessary cleansing steps, such as skipping introductory rows or dropping irrelevant columns before the main data structure is finalized.

Comparison: head() vs. tail()

While head() focuses on the beginning of the DataFrame, its natural counterpart, the tail() function, performs the exact opposite: it returns the last n rows. Understanding the distinction and knowing when to use each is essential for comprehensive data exploration.

The tail() function is particularly useful in time-series data or logs, where the most recent entries are often placed at the end of the file. By using tail(), analysts can confirm that the latest data points have been successfully appended or loaded. Similar to head(), tail() also defaults to returning the last five rows and accepts the integer argument n for customized selection.

Together, head() and tail() provide a powerful mechanism for sanity checking both ends of a dataset. For example, if you suspect data corruption might have occurred during the transmission or appending process, checking both df.head() and df.tail() can quickly pinpoint whether the issue is systemic (affecting all data) or localized (affecting only the beginning or the end). Both functions are cornerstones of initial data quality assessment within the Pandas ecosystem.

Conclusion and Further Exploration

The head() function is a simple yet profoundly powerful method for initial data inspection in Pandas. Its ability to quickly return a subset of the data, customize the number of rows, and apply effectively to both DataFrames and Series makes it an indispensable tool for any data professional utilizing Python for data processing.

By integrating head() early and often into your data manipulation workflows, you ensure better data quality, faster debugging cycles, and a more intuitive understanding of the underlying data structure. Continued practice with these basic exploration techniques is key to mastering the Pandas library.

The following tutorials explain how to perform other common functions in pandas:

Cite this article

stats writer (2025). How to Easily Preview DataFrames with Pandas head(). PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-use-pandas-head-function-with-examples/

stats writer. "How to Easily Preview DataFrames with Pandas head()." PSYCHOLOGICAL SCALES, 4 Dec. 2025, https://scales.arabpsychology.com/stats/how-to-use-pandas-head-function-with-examples/.

stats writer. "How to Easily Preview DataFrames with Pandas head()." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-to-use-pandas-head-function-with-examples/.

stats writer (2025) 'How to Easily Preview DataFrames with Pandas head()', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-use-pandas-head-function-with-examples/.

[1] stats writer, "How to Easily Preview DataFrames with Pandas head()," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, December, 2025.

stats writer. How to Easily Preview DataFrames with Pandas head(). PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top