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

Shazra H 27 Jul, 2023

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.

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

print(df.tail(2))``````

Output

## 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

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

tail(df, n = 2)``````

Output

## 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.

### 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!

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