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

Shazra H Last Updated : 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

print(df.head(3))

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

print(df.tail(2))

Output

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)

Output

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.

Purpose

head()tail()
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.

Focus

head()tail()
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.

Usage

head()tail()
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

head()tail()
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.

Efficiency

head()tail()
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

head()tail()
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

head()tail()
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

head()tail()
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

head()tail()
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.

Conclusion

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.

Extremely motivated content enthusiast with 6+ years of experience creating 100% plagiarism-free and well-researched content. Devoted to SEO Copywriting while ensuring unique quality to help brands drive successful businesses online.

Responses From Readers

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details