head () and tail () Functions Explained with Examples and Codes

Shazra H 27 Jul, 2023
4 min read

The head and tail functions are essential data analysis and programming tools, notably in the context of Python’s popular pandas package. This article deeply delves into the head and tail functions, with illustrated code examples in Python, R, and other related programming languages, demonstrating their importance in diverse data analysis contexts.

What is head() Function?

The head() function is primarily used to view the first few rows of a dataset. It helps users quickly get an overview of the data and its structure. Analysts can check column names, data types, and the data itself by displaying the initial records. The head() function is available in many programming languages, including Python and R.

What is tail() Function?

The tail() function offers a rapid view of the final few rows of a dataset, just like the head() does. It is especially helpful when working with huge datasets because it enables users to check that the data is full and spot any trends or outliers at the dataset’s end.

Example of head() and tail() in Python (using Pandas)

import pandas as pd

# Creating a sample DataFrame

data = {'Name': ['Ankit', 'Bhavya', 'Charvi', 'Diya', 'Eesha'],

        'Age': [25, 30, 22, 28, 35],

        'City': ['New York', 'London', 'Paris', 'Tokyo', 'Sydney']}

df = pd.DataFrame(data)

# Printing the first 3 rows using head() in Python


# Printing the last 2 rows using tail() in Python



Example of head() and tail() in Python (using Pandas)

Example of head() and tail() in R (using base R)

# Creating a sample data frame

data <- data.frame(Name = c("Ankit", "Bhavya", "Charvi", "Diya", "Eesha"),

                   Age = c(25, 30, 22, 28, 35),

                   City = c("New York", "London", "Paris", "Tokyo", "Sydney"))

# Printing the first 3 rows using head() in R

head(df, n = 3)

# Printing the last 2 rows using tail() in R

tail(df, n = 2)


Example of head() and tail() in R (using base R)

Difference Between head () and tail () Functions

The head() function in pandas displays the top rows of a DataFrame, while the tail() function shows the bottom rows. Both functions are used to get a quick overview of the data’s structure and contents, making them essential tools for data exploration and analysis in Python.


Shows the first few rows of a dataset, giving an overview of the beginning of the data.Displays the last few rows of a dataset, offering insights into the end of the data.


Primarily focuses on the initial records, making it useful for understanding the data’s structure and column names.Concentrates on the concluding records, helping users verify data completeness and identify patterns or outliers at the end.


Useful when users want to get a quick glimpse of the dataset’s content without scrolling through the entire dataset.Valuable for quick checks at the end of large datasets, where scrolling is impractical.

Data Exploration

Enables analysts to identify the first few data points, sample values, and any data entry issues or inconsistencies.Helps analysts check the final data points, validate data integrity, and observe any trends or patterns at the end of the dataset.


Generally faster to execute, as it only needs to retrieve and display the initial rows.It might take longer for larger datasets, as it needs to navigate through the entire dataset to access the last rows.

Visual Inspection

Useful for visualizing the data from the beginning, making it ideal for understanding its overall format.Ideal for visually inspecting the data’s conclusion, allowing users to observe the data tail

Handling Large Datasets

Efficient for examining the initial data rows in large datasets, essential for quick data profiling.Beneficial for analyzing the final data entries in large datasets, facilitating data completeness checks.

Common Usage

Commonly used for exploratory data analysis, understanding data structures, and checking data quality.Frequently used for time-series data to observe the most recent entries or for checking data captured at the end of a collection period

Programming Libraries

Available in various programming languages, including Python (pandas), R, and other data analysis libraries.Also present in the same programming languages and libraries that support the head() function.

Complementary Functions

head and tail: The two functions work together to give a complete picture of the dataset from the beginning and end, respectively.


In Python and R, the head tail methods are useful resources for inspecting data. To comprehend the structure of the data and make wise judgements, they enable users to visualize the beginning and end of datasets rapidly. Head and tail are useful functions in your data analysis workflow, whether working with tiny or huge datasets.

Want to learn more python functions? Sign-up for our free course on Introduction to Python!

Frequently Asked Questions

Q1. What do head() and tail() do in Python?

A. Python libraries like pandas and R include functions like tail and head. They are used to see the initial few rows (head) and last few rows (tail) of a dataset, giving an overview of the content and organization of the data.

Q2. What does head() do in Python?

A. In Python, the head() function displays the initial rows of a dataset, allowing users to inspect the data and understand its layout quickly.

Q3. What is tail() in Python pandas?

A. In Python pandas, the tail() function shows the last few rows of a dataset, providing insights into the concluding records.

Q4. What is head() in Python pandas?

A. In Python pandas, the head() function displays the first few rows of a DataFrame or Series, helping users see the data’s structure.

Shazra H 27 Jul, 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers